Thursday, 18 September 2014

Using Custom Fonts in Android

Using Custom Fonts in Android
                                     


Here you are going to know that how to use external fonts in Android

1.Open Eclipse IDE.
2.Click File -> New -> Project -> Android Application Project.
3.Enter the name of the application  as openactivity and click next .
4.Make sure that you have enabled the create project in workspace.
5.Design application launcher icon and create a blank activity workspace and click finish 

Now you need to design your xml file .
Activity_main.xml
Here is the coding of activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    tools:context="com.example.androidcustomfont.MainActivity" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="@string/mobile"
        android:textSize="35sp" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView1"
        android:gravity="center"
        android:text="@string/And"
        android:textSize="35sp" />

</RelativeLayout>

Asset
 We are going to access the fonts using this asset folder ,create a new folder under asset and named it as fonts and copy the font you needed  inside the fonts folder as shown below.
                                               
String.xml
The string.xml file is shown below the value for the textview is passed through the string.
<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="app_name">AndroidCustomFont</string>
    <string name="hello_world">Hello world!</string>
    <string name="action_settings">Settings</string>
    <string name="mobile">Mobileapptechnology</string>
    <string name="And">Android</string>

</resources>



MainActivity.java
Here the coding of MainActivity.java here we going to set the fonts to the textview which we copied it in the fonts folder under asset. 
package com.example.androidcustomfont;

import android.support.v7.app.ActionBarActivity;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;


public class MainActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        TextView tx = (TextView)findViewById(R.id.textView1);
        TextView tx1 =(TextView)findViewById(R.id.textView2);
        Typeface tf1= Typeface.createFromAsset(getAssets(), "fonts/complex.ttf");
        tx1.setTypeface(tf1);
        Typeface tf= Typeface.createFromAsset(getAssets(), "fonts/Disguise.otf");
        tx.setTypeface(tf);
    }


    }
The typeface class specify the typeface and style of the font like 
text size and specify how the text appears in the UI.
Now Run the application in emulator you will get the output as 
shown below.
                                          

             


1 comments:

Post a Comment

Total Pageviews

Contact Form

Name

Email *

Message *