15

I have to show a text in three parts, so i have used Html.fromHtml like this:

txtvw.setText(Html.fromHtml("<p align=right> <b> "
                    + "Hi!" + " </br> <font size=6>"
                    + " How are you "+"</font> </br>"
                    + "I am fine" + "  </b> </p>"));

The HTML is correct but in device it's showing in one line.

my xml declaration of textview is:

   <RelativeLayout
    android:id="@+id/Home"
    android:layout_width="fill_parent"
    android:layout_height="60dp"
    android:background="@drawable/transparentfooter"
    android:layout_above="@+id/bottombar" >


     <TextView 
    android:id="@+id/txt"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:textColor="@android:color/white"/>

    </RelativeLayout>
Vishal Pawar
  • 4,324
  • 4
  • 28
  • 54
zaiff
  • 999
  • 2
  • 13
  • 29

4 Answers4

17

The way you used the <br> tag is inappropriate. Use the following:

txtvw.setText(Html.fromHtml("<p align=right> <b> "
            + "Hi!" + " <br/> <font size=6>"
            + " How are you "+"</font> <br/>"
            + "I am fine" + "  </b> </p>"));

It should be <br/> and not </br> I have tested this code and it displays the 3 lines as expected.

Arun George
  • 18,352
  • 4
  • 28
  • 28
  • 1
    yes, it's working but the align right is not working, Can you tell me what's wrong. – zaiff Sep 24 '12 at 14:09
  • 2
    It seems like alignment is not supported in HTML text in TextView. You may try some of the trick from the following links to get your alignment. I would suggest that the best way would be to use `Gravity`. Try using 'txtvw.setGravity(Gravity.RIGHT)'. Check the following links: http://stackoverflow.com/questions/3874999/alignment-in-html-fromhtml and http://stackoverflow.com/questions/3235131/set-textview-text-from-html-formatted-string-resource-in-xml – Arun George Sep 25 '12 at 07:21
2
Html.fromHtml(String source)

now Deprecated from Api-24.

From Api-24 this method changed to

Html.fromHtml(String source,int flags)

So we can use like below from Api 24

txtvw.setText(Html.fromHtml("<p align=right> <b> "
            + "Hi!" + " <br/> <font size=6>"
            + " How are you "+"</font> <br/>"
            + "I am fine" + "  </b> </p>"),Html.FROM_HTML_MODE_LEGACY);
Lins Louis
  • 2,393
  • 2
  • 25
  • 30
1

Set lines tag to your layout android:lines="4"

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:lines="4"                
        android:text="@string/hello_world"
        tools:context=".MainActivity" />

</RelativeLayout>

Write correct "br" html tags

 TextView text =(TextView)findViewById(R.id.text);        
        text .setText(Html.fromHtml("<p align=right> <b> "
                + "<br>" +"Hi!" + "  </br> "
                + "<br> How are you "+" </br>"
                + "<br>I am fine" + " </br> </b> </p>"));
Yahor10
  • 2,123
  • 1
  • 13
  • 13
0

textView.setText(Html.fromHtml(str, Html.FROM_HTML_MODE_COMPACT));

Sumit Singh
  • 1,150
  • 13
  • 17