posengdev
posengdev

Reputation: 1

How to Add HTML to TextView?

How to add html page on TextView final TextView titleTextView = (TextView)?

private void renderViewIngredients()
{
    // reference
    final TextView titleTextView = (TextView) mRootView.findViewById(R.id.fragment_ebook_detail_ingredients_title);
    final ViewGroup containerViewGroup = (ViewGroup) mRootView.findViewById(R.id.fragment_ebook_detail_ingredients_container);
    final Button clearButton = (Button) mRootView.findViewById(R.id.fragment_ebook_detail_ingredients_clear);
    final Button recalculateButton = (Button) mRootView.findViewById(R.id.fragment_ebook_detail_ingredients_recalculate);

    // title
    if(mRecalculatedServings==mRecipe.getServings())
    {
        titleTextView.setText(R.string.fragment_ebook_detail_ingredients_title);
    }
    else
    {
        titleTextView.setText(getString(R.string.fragment_ebook_detail_ingredients_title_recalculated, mRecalculatedServings));
    }

Upvotes: 0

Views: 55

Answers (2)

Skizo-ozᴉʞS ツ
Skizo-ozᴉʞS ツ

Reputation: 20656

How to add html page on TextView

Since you are getting the String from strings.xml I recommend you to use the CDATAsection, as follow:

<string name="fragment_ebook_detail_ingredients_title"><![CDATA[<p><b>YourEbookDetailWithBold</b><p>]]></string>

Then at the time you want to use it you have to use the Html.fromHtml()

titleTextView.setText(Html.fromHtml(getString(R.string.fragment_ebook_detail_ingredients_title)));

Edit

When I saw :

How to add html page on TextView

I thought about a link html, so if you have a link follow these steps if not don't read more.

Make sure that your TextView in your xml has this

android:linksClickable="true"

Then you'll have to create a Spanned

Spanned spannedlink = Html.fromHtml(getString(R.string.fragment_ebook_detail_ingredients_title));

Then add this line :

titleTextView.setMovementMethod(LinkMovementMethod.getInstance());

And set text on titleTextView

titleTextView.setText(spannedlink );

Upvotes: 1

SuperFrog
SuperFrog

Reputation: 7674

If you want to use html tags with a TextView you should use:

titleTextView.setText(Html.fromHtml("your html string"));

Upvotes: 0

Related Questions