Pages

Monday, July 23, 2012

Styling text in ANDROID through XML

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.
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
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
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
<?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.
1
2
3
4
5
<?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

No comments:

Post a Comment

Popular Posts