Android different text styles in TextView

When you create Android application, you might want to apply different styles to text that displays in the TextView. For example, you can apply different colors to text, make the text smaller or bigger, and italic or bold, and display links to the user. In Android, you can use the fromHtml of Html class to apply different style to text in the TextView. This method accepts a string that can contain html tags. The html tags supported by the fromHtml method are listed below:


<a href="...">
<b>
<big>
<blockquote>
<br>
<cite>
<dfn>
<div align="...">
<em>
<font size="..." color="..." face="...">
<h1>
<h2>
<h3>
<h4>
<h5>
<h6>
<i>
<img src="...">
<p>
<small>
<strike>
<strong>
<sub>
<sup>
<tt>
<u>

Now to get start, you need to create a new Android project. After the project is created, you will modify the main_activity.xml file to add a TextView as shown below.

<LinearLayout 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"
   android:orientation="vertical"
   android:paddingBottom="@dimen/activity_vertical_margin"
   android:paddingLeft="@dimen/activity_horizontal_margin"
   android:paddingRight="@dimen/activity_horizontal_margin"
   android:paddingTop="@dimen/activity_vertical_margin"
   tools:context=".MainActivity" >
  <TextView
     android:id="@+id/txtfield"
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"
     android:layout_marginLeft="10dp"
     android:layout_marginRight="10dp"
      />
  
</LinearLayout>


In the onStart method of the MainActivity class, you will create a reference to the TextView using findViewById method, define text that has different styles using html tags, supply the text to fromHtml method, and set the returned string from the fromHtml method to the TextView with the setText method. When you define a link by using <a> tag, the link is not clickable. To make it clickable, you need to call the setMovementMethod of TextView instance.

  package com.example.andtip;

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



public class MainActivity extends Activity {


   protected void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.activity_main);
      }


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

return true;
    
   }
  
   public void onStart(){
   super.onStart();
   TextView txtview=(TextView)findViewById(R.id.txtfield);
   String redText="<p><font color='#ff0000'>This is red text.</p>";
   String redTextBig="<p><font color='#00ff'><big>This is big blue text.</big></font></p>";
   String italicText="<p><i>This is itatlic text.</i></p>";
   String linkText="<p><a href='http://www.worldbestlearningcenter.com'> Open www.worldbestlearningcenter.com website.</a></p>";
   String textStyled=redText+redTextBig+italicText+linkText;
   //set text containing html tags to TextView
   txtview.setText(Html.fromHtml(textStyled));
   //make the link clickable
   txtview.setMovementMethod(LinkMovementMethod.getInstance());
  
   }
}

Android-different-text-styles-in-TextView

Posted by: Dara | post date: 05-23-2014 | Subject: Android Apps Development

write programming tip



This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.