Friday 16 May 2014

How to set custom font for text view or button in android

To have your own customized application of your own theme make your text look your style with custom font for your textviews and buttons.

You can check this link for custom font for spinners in android.

For custom fonts for textviews and buttons follow the simple steps given in this post.



Step 1 : In your Layout file place some text views and buttons as required for your app.

Step 2 : Step 2: Create a folder in Assets and name it as fonts. Then paste your typeface (fontname.ttf) file in fonts folder.

Step 3 : Create a typeface of your font and assign it for your buttons and textviews in your mainactivity.

MainActivity.java :

package com.exam.sample;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

Typeface Font = Typeface.createFromAsset(getAssets(),"fonts/idr.ttf");

TextView tv1 = (TextView)findViewById(R.id.textView1);
TextView tv2 = (TextView)findViewById(R.id.textView2);
Button bt1 = (Button)findViewById(R.id.button1);

tv1.setTypeface(Font);
tv2.setTypeface(Font);
bt1.setTypeface(Font);
}

No comments:

Post a Comment