logo

Every Android device comes with a collection of standard fonts: Droid Sans, Droid Sans Mono and Droid Serif. They were designed to be optimal for mobile displays, so these are the three fonts you will be working with most of the time and they can be styled using a handful of XML attributes. You might, however, see the need to use custom fonts for special purposes. We’ll be taking a look at that as well.

Font Style Attributes

In the following section we’re going to examine the different XML attributes that you can use to style components with text.

Typeface

As stated in the overview, there are three different default typefaces which are known as the Droid family of fonts: sans, monospace and serif. You can specify any one of them as the value for the android:typeface attribute in the XML declaration of a component that supports text styling, such as TextView. Here’s an example of all three typefaces in action:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
	
	<TextView android:layout_width="fill_parent" 
	    android:layout_height="wrap_content" 
	    android:text="@string/hello"/>
	
	<TextView android:layout_width="fill_parent"  
	    android:layout_height="wrap_content" 
	    android:text="I am Normal"
	    android:layout_marginLeft="8dp"
	    android:layout_marginTop="8dp"/>
    
    <TextView android:layout_width="fill_parent" 
	    android:layout_height="wrap_content" 
	    android:text="I am Serif"
	    android:typeface="serif"
	    android:layout_marginLeft="8dp"
	    android:layout_marginTop="8dp"/>
	    
	<TextView android:layout_width="fill_parent" 
	    android:layout_height="wrap_content" 
	    android:text="I am Sans"
	    android:typeface="sans"
	    android:layout_marginLeft="8dp"
	    android:layout_marginTop="8dp"/>    

    <TextView android:layout_width="fill_parent" 
	    android:layout_height="wrap_content" 
	    android:text="I am Monospace"
	    android:typeface="monospace"
	    android:layout_marginLeft="8dp"
	    android:layout_marginTop="8dp"/>
</LinearLayout>

In addition to the above, there is another attribute value named “normal” which defaults to the sans typeface.

DROID family & normal font

Using Custom Fonts

We’ll use Garamond.TTF font for demonstration purposes. Download it and place the TTF file in the ./assets directory (create it if it doesn’t exist yet).
We’re going to use a basic layout file with a TextView, marked with an id of “custom_font” so we can access it in our code.

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

      <TextView android:id="@+id/custom_font" 
	 	android:layout_width="fill_parent" 
	        android:layout_height="wrap_content" 
	        android:text="This is garamond Font"/> 

Open your main activity file and insert this into the onCreate() method:

TextView t=(TextView)findViewById(R.id.custom_font);
        Typeface font=Typeface.createFromAsset(getAssets(), "Garamond.TTF");
        t.setTypeface(font);

The Typeface class contains a static builder method createFromAsset, which takes an AssetManager as its first parameter and a path to the file in the second argument. We’re handing it the default asset manager and the name of the font file because it’s located in the root of the “assets” folder. Once we’ve got an instance of our custom typeface, all that’s left is a call to TextView’s setTypeface() method.
[/caption]>

Text Style

The android:textStyle

attribute can be used to put emphasis on text. The possible values are: normal, bold, italic. You can also specify bold|italic.

<TextView  
            android:layout_width="fill_parent"  
            android:layout_height="wrap_content"  
            android:text="This is bold!"  
            android:textStyle="bold"  />

Text Style

Text Size

android:textSize specifies the font size. Its value must consist of two parts: a floating-point number followed by a unit. Available units are: sp (scaled pixels), px (pixels), dp (density-independent pixels), in (inches), mm (millimeters). It is generally a good practice to use the sp unit so that it will be consistent across all screen resolutions.

<TextView  
        android:layout_width="fill_parent"  
        android:layout_height="wrap_content"  
        android:text="15sp is the 'normal' size."  
        android:textSize="15sp" />

Text Size

Text Color

The android:textColor attribute’s value is a hexadecimal RGB value.

    <TextView  
            android:layout_width="fill_parent"  
            android:layout_height="wrap_content"  
            android:text="A light blue color."  
            android:textColor="#00ccff"  
            />  

AUTHOR: Vikas Hiran
No Comments

Leave a Comment

Your email address will not be published.