Cdacians

Cdacians
Cdacians

Sunday, 26 February 2012

Type face Example in Android


Description:
This example shows you can use your own typeface font and use into your application.
Algorithm:
1.) Create a new project by File-> New -> Android Project name it TypefaceExample.
2.) Write following code into res/layout.main.xml:
<?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/CustomFontText"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:textSize="30sp"
       android:text="Typeface Activity!!!! This is font Typeface example">
        </TextView>
</LinearLayout>
3.) Create a fonts folder into assets directory and put your font file i.e. “.otf” file there.
4.) Run for output.
Steps:
1.) Create a project named TypefaceExample and set the information as stated in the image.
Build Target: Android 2.1
Application Name: TypefaceExample
Package Name: com.org.typefaceexample
Activity Name: TypefacActivity
Min SDK Version: 7
2.) Open TypefaceActivity.java file and write following code there:
package com.org.typefaceexample;
import org.pack.R;
import android.app.Activity;
import android.graphics.Typeface;
import android.os.Bundle;
import android.widget.TextView;
public class TypefaceActivity extends Activity {
        @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Typeface tf = Typeface.createFromAsset(getAssets(),
                "fonts/File.otf");
        TextView tv = (TextView) findViewById(R.id.CustomFontText);
        tv.setTypeface(tf);
    }
}
3.) Compile and build the project.
Output

Thanks
akm
www.cdacians.com

No comments:

Post a Comment