Reputation:
For implementing custom font i seen few examples here issue is different,I am taking custom font in one abstract class which is used in all over the application, Here is my code
public abstract class X extends Activity implements OnClickListener {
private Vibrator vibrator;
private TextView TV_score;
private TextView TV_hints;
private ImageButton BTN_back;
// Font path
private String fontPath = "fonts/CarterOne.ttf";
Typeface tf = Typeface.createFromAsset(getAssets(), fontPath);
public static Context context;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Logger.log("onCreate " + this.getClass().getName());
vibrator = (Vibrator) this.getSystemService(VIBRATOR_SERVICE);
context = getBaseContext();
SoundHandler.getInstance().initSounds(context);
}
tried by debugging Here i am getting null pointer exception
Typeface tf = Typeface.createFromAsset(getAssets(), fontPath);
how to resolve this problem give some suggestion .
Upvotes: 2
Views: 539
Reputation: 563
In android , you can define your own custom fonts for the strings in your application. You just need to download the required font from the internet, and then place it in assets/fonts folder.
After putting fonts in the assets folder under fonts folder, you can access it in your java code through Typeface class. First , get the reference of the text view in the code. Its syntax is given below:
TextView tx = (TextView)findViewById(R.id.textview1);
The next thing you need to do is to call static method of Typeface class createFromAsset() to get your custom font from assets. Its syntax is given below:
Typeface custom_font = Typeface.createFromAsset(getAssets(), "fonts/font name.ttf");
The last thing you need to do is to set this custom font object to your TextView Typeface property. You need to call setTypeface() method to do that. Its syntax is given below:
tx.setTypeface(custom_font);
Upvotes: 0
Reputation: 6118
create Typeface object in onCreate method of activity
public abstract class X extends Activity implements OnClickListener {
private Vibrator vibrator;
private TextView TV_score;
private TextView TV_hints;
private ImageButton BTN_back;
// Font path
private String fontPath = "fonts/CarterOne.ttf";
Typeface tf ;
public static Context context;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Logger.log("onCreate " + this.getClass().getName());
vibrator = (Vibrator) this.getSystemService(VIBRATOR_SERVICE);
context = getBaseContext();
SoundHandler.getInstance().initSounds(context);
tf = Typeface.createFromAsset(getAssets(), fontPath);
}
Upvotes: 1