Tuesday, January 29, 2013

Android custom fonts part-2

There are several ways in android to prepare custom fonts. i hvav use two of them in the same code example please have a look where you can make a project with the name CustomFont.

1) 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" >

    <com.jitesh.customfonts.CustomTextView
        android:id="@+id/custom1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="70dp"
        android:text="@string/hello_Jitesh"
        tools:context=".MainActivity" >
    </com.jitesh.customfonts.CustomTextView>

    <TextView
        android:id="@+id/custom"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="@string/hello_Jitesh_second"
        tools:context=".MainActivity" />

    <TextView
        android:id="@+id/custom2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_marginBottom="67dp"
        android:text="@string/hello_Jitesh_third"
        tools:context=".MainActivity" />

</RelativeLayout>

2) MainActivity.java


package com.jitesh.customfonts;

import android.os.Bundle;
import android.app.Activity;
import android.graphics.Typeface;
import android.view.Menu;
import android.widget.TextView;

public class MainActivity extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView tv = (TextView) findViewById(R.id.custom);
Typeface face = Typeface.createFromAsset(getAssets(),
"fonts/HandmadeTypewriter.ttf");

tv.setTypeface(face);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}


the CustomTextView .java code is as follows


package com.jitesh.customfonts;

import android.content.Context;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.widget.TextView;

public class CustomTextView extends TextView {

public CustomTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}

public CustomTextView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}

public CustomTextView(Context context) {
super(context);
init();
}

public void init() {

Typeface tf = Typeface.createFromAsset(getContext().getAssets(),
"fonts/HandmadeTypewriter.ttf");
setTypeface(tf, 1);

}
}



3)You need to put yours custom font inside asset folder inside font directory please click at the given below link for the code


No comments:

Post a Comment