Blog

Android: Adding styling to a text (bold italic etc..)

Settings the text in android is done by finding the object you wish to change, and then using its setText property. Below shows my code updating a textview by its id score_text to say “boring regular text”

((TextView)this.findViewById(R.id.score_text)).setText("boring regular text \n")

This is good but what if you want to include styling in your text, so bold emphasis or italics. Well it’s very simple we first add the android.text.html include:

import android.text.Html;

and then surround our code in Html.fromHtml() this causes them html styling to modify our text.

((TextView)this.findViewById(R.id.score_text)).setText(Html.fromHtml("excitingand cooltext
"

Below shows a screenshot of the regular styling and the html styling in an android application.

styled android text

styled and regular android text

6 Comments

  1. vani

    Hi

    In addition to the text styles(like bold n italic), can we get the list types like bullets and number types.Is there any chance to get the numbers and bullets by adding the respective html tags.

    Thank you!

    Regards,
    Bhavani.G

  2. Lacy

    Thanks, useful info.

  3. Michael

    Thanks very much. This is the only Gridview I got working now.

    This is my res/layou/main.xml:

    and this is how I call it in the Activity:
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    DisplayMetrics metrics = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(metrics);

    GridView gridview = (GridView)findViewById(R.id.gridView1);
    gridview.setAdapter(new MonthAdapter(this, 11, 2011, metrics));

    gridview.setOnItemClickListener(new OnItemClickListener() {
    public void onItemClick(AdapterView parent, View v, int position, long id) {
    Toast.makeText(CanTimeActivity.this, “” + position, Toast.LENGTH_SHORT).show();
    }
    });

  4. Javatechig

    Check out for working example using img tag

    http://javatechig.com/2013/04/07/how-to-display-html-in-android-view/

  5. yodymada

    How to make a textview have a justified alignment ??
    i’m sorry my english not good..

  6. paver usado curitiba

    Este é um tópico é perto de meu coração…
    Muito obrigado Exatamente onde são seus dados de contacto no entanto?

Leave a Comment

Your email address will not be published. Required fields are marked *