Android supported HTML Tags : How to Parse String with HTML Tags

HTML support of TexView
will add glossy look to your web app. You can’t avoid html tags when you working
with native web capabilities like hyper-link on text, bold text etc. You may
familiar with textview but have you tried any HTML tag along with it.
Android supported HTML Tags
This tutorial discusses how to use html tags in
TextView of android app. If you want to use any HTML tag dynamically in android
you should import a package android.text.Html.
I consistently notice amongst few fellow android developers
who follow HTML tags well in TextView. Let me paint some examples for you.

Android
Supported HTML Tags : strings.xml

·  <big></big>
·  <small></small>
·  <bold></bold>
·  <strike></strike>
·  <a href=””></a>
·  <sup></sup>
·  <sub></sub>
·  <u></u>
·  <i></i>
·  <tt></tt>
Example: strings.xml 
<?xml version="1.0" encoding="utf-8"?>
    <resources>
    <string name="HtmlData">
        <big>Hello Iam Big</big> \n
        <small>Iam Small</small>  \n
        <b>Iam bold</b> \n
        <strike>Xamarin 100$</strike>\n
        <a href="http://Xamarin.com">Goto Xamarin</a>
        X<sub>2</sub>Y \n
        X<sup>30</sup>Z \n
        <u>Underline</u> \n
        <i>Hi Five </i> \n
        <tt>Hi Six</tt>\n
   </string>
   </resources>
HtmlTest.cs
using Android.App;
using Android.OS;
using Android.Text;
using Android.Widget;

namespace Xamarin.Droid
{
	[Activity(Label = "HtmlTest")]
	public class HtmlTest : Activity
	{
		TextView dynamicHTML;
		protected override void OnCreate(Bundle savedInstanceState)
		{
			base.OnCreate(savedInstanceState);

			SetContentView(Resource.Layout.activity_html_test);
			dynamicHTML = (TextView)FindViewById(Resource.Id.dynamicHTML);
			dynamicHTML.SetText(Html.FromHtml("I like Xamarin
 Strong Data
Moving
"), TextView.BufferType.Spannable);
		}

		public override bool OnCreateOptionsMenu(Android.Views.IMenu menu)
		{
			MenuInflater.Inflate(Resource.Layout.activity_html_test, menu);
			return true; 
		}
	}
}

activity_html_test.axml
<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/Data"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="@string/HtmlData" />

    <TextView
        android:id="@+id/dynamicHTML"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/Data"
        android:layout_below="@+id/Data"
        android:layout_marginTop="16dp"
        android:text="TextView" />

</RelativeLayout>

One Comments

  • anup April 22, 2017 Reply

    Awesome blog..

Leave a Reply

Read previous post:
The Good and The Bad of Xamarin Mobile Development

When considering iOS or Android app development, most of us think about Objective-C/Swift, and Java first of all. Being considered...

Close