1

I don't want to use the default font for my app.

I want to use my desired font.

Therefore, I copied my desired font in assets/fonts/ folder and coded as shown below.

However, an error occurs.

Please help me use the same font for the full app.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    LinearLayout ll = new LinearLayout(this);
    ll.setBackgroundResource(R.drawable.bg);
    this.setContentView(ll);
    TextView tv = (TextView) findViewById(R.id.app_name);
    Typeface face = Typeface.createFromAsset(getAssets(),
                    "fonts/TAU_MADN.TTF");
    tv.setTypeface(face);

UPDATE:

My xml TextView code;

  <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="app_name"
        android:textStyle="bold" />

Error:

error: Error: String types not allowed (at 'id' with value 'app_name').

4 Answers4

0

Please make sure that the path has been given correctly, like case sensitives stuffs including the extension.TTF or .ttf {same name as in the fonts folder} including the folder name "FONTS"!.

Exceptional
  • 2,994
  • 1
  • 18
  • 25
0

Your error has nothing to do with the loading of the custom font as an asset.

In your layout R.layout.activity_main there is no TextView with id: R.id.app_name, therefore when you initialize that TextView in your code the error will thrown.

Make sure you have such TextView in your layout.

If you think you do have that TextView in the layout already, try and clean the project.

Edit

Add the id as below:

android:id="@+id/app_name"

Also rename your font with lowercased ".ttf" extension, and the reference to it all the same.

Typeface face = Typeface.createFromAsset(getAssets(),
                    "fonts/TAU_MADN.ttf"); // lowercased ".ttf" reference

See here for more info on your issue.

Community
  • 1
  • 1
Mena
  • 47,782
  • 11
  • 87
  • 106
0

May this help you:

Change android:id attribute in your TextView Like:

<TextView
        android:id="@+id/app_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textStyle="bold" />

And also change this line in your code:

Typeface face = Typeface.createFromAsset(getAssets(),
                    "fonts/TAU_MADN.ttf");
Bhavin Nattar
  • 3,189
  • 2
  • 22
  • 30
0

Android allows integer type ids only

You mention string type id thats your mistake

Change this Line

android:id="app_name"

to

android:id="@+id/app_name"
Venky
  • 226
  • 1
  • 5