Styling text in ANDROID through XML.

By | June 19, 2011

Hello….

In today’s tutorial I will show you how to style your text in ANDROID.
This is a simple example to show how styling is done using XML in android.
This will also show you how to get a string from the resources in java code.

First create a new project and name it “StyleTextDemo” and copy this code into it.

package pack.coderzheaven;

import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.os.Bundle;
import android.widget.TextView;

public class StyleTextDemo extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        super.onCreate(savedInstanceState);

        setContentView(R.layout.resources);

        TextView tv;
        CharSequence cs;
        String str;

        // Using the getString() conevenience method, retrieve a string
        // resource that happens to have style information.  Note the use of
        // CharSequence instead of String so we don't lose the style info.
        cs = getText(R.string.styled_text);
        tv = (TextView)findViewById(R.id.styled_text);
        tv.setText(cs);

        // Use the same resource, but convert it to a string, which causes it
        // to lose the style information.
        str = getString(R.string.styled_text);
        tv = (TextView)findViewById(R.id.plain_text);
        tv.setText(str);

        Context context = this;

        // Get the Resources object from our context
        Resources res = context.getResources();

        // Get the string resource, like above.
        cs = res.getText(R.string.styled_text);
        tv = (TextView)findViewById(R.id.res1);
        tv.setText(cs);
    }
}

Now in the “main.xml” file copy the following code. This is simply for the layout

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical">

   <TextView
        android:id="@+id/coderz"
        android:text="@string/coderz"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:textStyle="normal"
        android:padding="10dip"
        />
    <TextView
        android:id="@+id/styled_text"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:textStyle="normal"
        />

    <TextView
        android:id="@+id/plain_text"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:textStyle="normal"
        />

    <TextView
        android:id="@+id/res1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:textStyle="normal"
        />
</LinearLayout>

Now the third step in the strings.xml file, place this code.

<?xml version="1.0" encoding="utf-8"?>
<resources>
     <string name="coderz"><b><i>Style Text Demo from CoderzHeaven, Enjoy</i></b></string>
     <string name="styled_text">Plain, <b>bold</b>, <i>italic</i>, <b><i>bold-italic</i></b></string>
</resources>

You are done, Now go on and run it.
Enjoy
Please leave your comments on this post and encourage us.

Style Text Demo in android

Style Text Demo in android

2 thoughts on “Styling text in ANDROID through XML.

  1. krishna sharma

    Your tutorial are awesome and to the point , I am enjoying them , please don’t stop making them,If you will make a tutorial for offline speech recognition , I would be very greatfull to you, and thanks a lot for the previous tutorial 🙂

    Reply
  2. Leila

    I am very new at any kind of programming and I am having trouble getting the code you wrote into Android Studio. I have two questions:
    1. In the dialogue box to create a new project, do I select ‘Add No Activity’ or ‘Blank Activity’ (‘Blank Activity’ being the one that displays ‘Hello World’)
    2. I cannot find the main.xml file.

    I’m really keen to follow the tutorial because I want to learn to style text.

    Thank You!

    Reply

Leave a Reply

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