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>
SetText(String, TextView+BufferType) Sets the text to be displayed using a string resource identifier.
Here is the simple solution to show HTML in TextView in android. Step 1 − Create a new project in Android Studio,go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml.
fromHtml. Returns displayable styled text from the provided HTML string. Any <img> tags in the HTML will use the specified ImageGetter to request a representation of the image (use null if you don't want this) and the specified TagHandler to handle unknown tags (specify null if you don't want this).
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.
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With