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

7:38 PM 1 Comments



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>Vanish 100$</strike>\n
        <a href="http://bigknol.com">Goto Bigknol</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.java


import android.app.Activity;
import android.os.Bundle;
import android.text.Html;
import android.view.Menu;
import android.widget.TextView;

public class HtmlTest extends Activity {

 TextView dynamicHTML;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_html_test);
     dynamicHTML=(TextView)findViewById(R.id.dynamicHTML);
  dynamicHTML.setText(Html.fromHtml("<font color='red'>I like bigknol</font><br />" +
    "<strong>Strong Data</strong><br />" +
    "<tt>Moving</tt><br />"));

 }

 @Override
 public boolean onCreateOptionsMenu(Menu menu) {
  // Inflate the menu; this adds items to the action bar if it is present.
  getMenuInflater().inflate(R.menu.activity_html_test, menu);
  return true;
 }

}
activity_html_test.xml

<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"
    tools:context=".HtmlTest" >

    <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>

Hi Nikinmon SK i like to share my knowledge !

1 comments:

kavita said...

Sir,
Am tackling with oneissue.Actually I have an lyrcis showing app for bhajans.I use sqlite file ,but during display all the bhajans thery are not coming in formatted ways,like line by line .same as whta I wrote in database file.So help me out ???Thanks you