romruben
romruben

Reputation: 194

Custom typeface error

I want to change the app typeface and I can't:

AndroidManifest.xml

...
    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" 
        android:theme="@style/CustomTheme"
        >
...

style.xml

....

    <style name="CustomTheme">
        <item name="android:typeface">stheiti.ttf</item>
    </style>
....

But I get an error:

error: Error: String types not allowed (at 'android:typeface' with value 'stheiti.ttf').

But I have a rootAPP/assets/stheiti.ttf

I do not understand why get that error, I searched in many sites the solution but i couldn't found any with same problem

Thanks in advance! :=)

Upvotes: 1

Views: 3245

Answers (4)

Khan
Khan

Reputation: 7605

For whole application u need to make custome TextView u can us as shown below for each and every textview of xml before going to this first make folder with naming fonts in asset in that fonts folder add ur font stheiti.ttf

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

 public class MyTextView extends TextView{

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

    }

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

    }

    public MyTextView(Context context) {
        super(context);
        init();

    }



    private void init() {
        if (!isInEditMode()) {
            Typeface tf = Typeface.createFromAsset(getContext().getAssets(), "fonts/stheiti.ttf");
            setTypeface(tf);
        }
    }


 }

after that for each and every textview in xml on which u want this type of font than ur textview in xml should be like this add other property of textview which u needed same as for normal textview

<YourProjectPackageName.MyTextView
    android:id="@+id/textview1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="22px"
    android:textColor="#34A4c5"
 ></YourProjectPackageName>

and in your activity use as shown below for custome textview

MyTextView textview=(MyTextView)findViewById(R.id.textview);

Upvotes: 0

rajpara
rajpara

Reputation: 5203

you can set custom font in text view like

TextView txt = (TextView) findViewById(R.id.custom_font);
Typeface font = Typeface.createFromAsset(getAssets(), "stheiti.ttf");
txt.setTypeface(font);

If you want to implement custom font in whole application then you can create your custom view and set your font at the initialize of that view.

public class MyTextView extends TextView {

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

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

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

public void init() {

    Typeface tf = Typeface.createFromAsset(getContext().getAssets(), "stheiti.ttf");
    setTypeface(tf ,1);

}

Upvotes: 1

Dipak Keshariya
Dipak Keshariya

Reputation: 22291

Use Below Code for that, it may help you.

// Font path
String fontPath = "fonts/trbuc.ttf";

// text view label
TextView txtGhost = (TextView) findViewById(R.id.mTxtViewCustomFont);

// Loading Font Face
Typeface tf = Typeface.createFromAsset(mContext.getAssets(), fontPath);

// Applying font
txtGhost.setTypeface(tf);

Upvotes: 0

Paresh Mayani
Paresh Mayani

Reputation: 128428

You can't do this, you have to set custom font to every view by programatically (Even you can't set custom typeface through XML, yes you can set but only built-in typefaces)

Check this: Adding custom Font to Theme in Android and Using a custom typeface in Android

Upvotes: 0

Related Questions