Skip to content Skip to sidebar Skip to footer

How To Make Compoundview

I have a scenario where I have a String which has HTML tag in it. In html tag,there can be many or one img tag.Now I have to show images embedded in text. For that I was using text

Solution 1:

So after some hours of digging i found some solution here is the code with some details.

This answer may help someone. I used Jsoup to extract <Img/> tag out of the string then i show the image in ImageView and <p> in Textview. Results was according to what i needed. Also i used Universal Image Loader Libaray to load images in ImageView Then i added view programmatically to the layout in my case layout was the linearlayout so i made a helper class and passed content,html string and linear layout as the parameter.

add jsoup in your project.

compile'org.jsoup:jsoup:1.9.2'

Here some snippet.

publicclassPostContentHandler {
Context context;
String content;
LinearLayout linearLayout;

publicPostContentHandler(Context context, String content, LinearLayout linearLayout) {
    this.context = context;
    this.content = content;
    this.linearLayout = linearLayout;

}

publicvoidsetContentToView() {

    //custom fontTypefacebitterBoldFont= Typeface.createFromAsset(context.getAssets(), "fonts/Bitter-Regular.otf");

    List<String> p = newArrayList<>();
    List<String> src = newArrayList<>();
    List<String> li = newArrayList<>();
    Documentdoc= Jsoup.parse(content);

    Elementselements= doc.getAllElements();

    for (Element element : elements) {
        Tagtag= element.tag();
        if (tag.getName().matches("h[1-6]{1}")) {
            Stringheading= element.select(tag.getName().toString()).text();
            TextViewtextView=newTextView(context);
            textView.setLayoutParams(newLinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
                    LinearLayout.LayoutParams.WRAP_CONTENT));
            LinearLayout.LayoutParamsparams= (LinearLayout.LayoutParams) textView.getLayoutParams();
            inttop= (int) context.getResources().getDimension(R.dimen.heading_margin_top);
            intstart= (int) context.getResources().getDimension(R.dimen.content_margin);
            intend= (int) context.getResources().getDimension(R.dimen.content_margin);
            params.setMargins(start, top, end, 0);
            textView.setTextSize(20);
            textView.setTypeface(bitterBoldFont);
            textView.setText(heading);
            textView.setTextColor(context.getResources().getColor(R.color.black));
            linearLayout.addView(textView);
        }
 if (tag.getName().equalsIgnoreCase("p")) {
            element.select("img").remove();
            Stringbody= element.html();
            TextViewtextView=newTextView(context);
            textView.setLayoutParams(newLinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
                    LinearLayout.LayoutParams.WRAP_CONTENT));
            LinearLayout.LayoutParamsparams= (LinearLayout.LayoutParams) textView.getLayoutParams();
            inttop= (int) context.getResources().getDimension(R.dimen.heading_margin_top);
            intstart= (int) context.getResources().getDimension(R.dimen.content_margin);
            intend= (int) context.getResources().getDimension(R.dimen.content_margin);
            params.setMargins(start, top, end, 0);
            textView.setTypeface(bitterBoldFont);
            textView.setLinksClickable(true);
            textView.setAutoLinkMask(Linkify.WEB_URLS);
            textView.setText(Html.fromHtml(body));
            textView.setTextColor(context.getResources().getColor(R.color.content_color));
            linearLayout.addView(textView);
            p.add(body);
        }
        if (tag.getName().equalsIgnoreCase("ol")) {
            Stringol= element.select(tag.getName().toString()).outerHtml();
            TextViewtextView=newTextView(context);
            textView.setLayoutParams(newLinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
                    LinearLayout.LayoutParams.WRAP_CONTENT));
            LinearLayout.LayoutParamsparams= (LinearLayout.LayoutParams) textView.getLayoutParams();
            params.setMarginStart((int) context.getResources().getDimension(R.dimen.content_margin));
            inttop= (int) context.getResources().getDimension(R.dimen.heading_margin_top);
            intstart= (int) context.getResources().getDimension(R.dimen.content_margin);
            intend= (int) context.getResources().getDimension(R.dimen.content_margin);
            params.setMargins(start, top, end, 0);
            textView.setTypeface(bitterBoldFont);
            textView.setLinksClickable(true);
            textView.setAutoLinkMask(Linkify.WEB_URLS);
            textView.setTextColor(context.getResources().getColor(R.color.content_color));
            textView.setText(Html.fromHtml(ol, null, newMyTagHandler()));
            linearLayout.addView(textView);

        }
        if (tag.getName().equalsIgnoreCase("ul")) {
            Stringul= element.select(tag.getName().toString()).outerHtml();
            TextViewtextView=newTextView(context);

            textView.setLayoutParams(newLinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
                    LinearLayout.LayoutParams.WRAP_CONTENT));
            LinearLayout.LayoutParamsparams= (LinearLayout.LayoutParams) textView.getLayoutParams();
            inttop= (int) context.getResources().getDimension(R.dimen.heading_margin_top);
            intstart= (int) context.getResources().getDimension(R.dimen.content_margin);
            intend= (int) context.getResources().getDimension(R.dimen.content_margin);
            params.setMargins(start, top, end, 0);
            textView.setTypeface(bitterBoldFont);
            textView.setLinksClickable(true);
            textView.setAutoLinkMask(Linkify.WEB_URLS);
            textView.setTextColor(context.getResources().getColor(R.color.content_color));
            textView.setText(Html.fromHtml(ul, null, newMyTagHandler()));
            linearLayout.addView(textView);
        }
        if (tag.getName().equalsIgnoreCase("img")) {
            Stringurl= element.select("img").attr("src");
            finalImageViewimageView=newImageView(context);
            imageView.setLayoutParams(newLinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
                    LinearLayout.LayoutParams.WRAP_CONTENT));
            finalProgressBarprogressBar=newProgressBar(context);
            linearLayout.addView(progressBar);
            progressBar.setVisibility(View.GONE);
            ImageLoaderimageLoader= ImageLoader.getInstance();
            imageLoader.displayImage(url, imageView, newSimpleImageLoadingListener() {
                @OverridepublicvoidonLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
                    super.onLoadingComplete(imageUri, view, loadedImage);
                    progressBar.setVisibility(View.INVISIBLE);
                    intheight= loadedImage.getHeight();
                    imageView.getLayoutParams().height = getScreenWidth();
                    imageView.setAdjustViewBounds(true);
                    imageView.requestLayout();
                }

                @OverridepublicvoidonLoadingStarted(String imageUri, View view) {
                    super.onLoadingStarted(imageUri, view);
                    progressBar.setVisibility(View.VISIBLE);
                }
            });
            linearLayout.addView(imageView);
            src.add(url);
        }

    }
}

publicstaticintgetScreenWidth() {
    return Resources.getSystem().getDisplayMetrics().widthPixels;
}
}

I hope my answer will help someone.

Post a Comment for "How To Make Compoundview"