Anuj
Anuj

Reputation: 367

Change font in android

I know this question has been asked many times but still i am not getting it right.So please do help me.I want to change the font of my textviews.I find some code on google and end up doing this

Code

public class SliderFont extends TextView {
    public SliderFont(Context context, AttributeSet attrs) {
        super(context, attrs);

        Typeface typeface = Typeface.createFromAsset(context.getAssets(), "fonts/redhead.ttf");
        setTypeface(typeface);
    }
}

And used this in my XMl

<pocketdocs.indiehustlers.com.pocketdocsv2.Utils.SliderFont
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:layout_marginLeft="5dp"
    android:text="AWAITED APPROVAL"
    android:textColor="#fff"
    android:textSize="@dimen/edit_text_size" />

But still I am not getting the desired font.

I have created assets folder like this enter image description here

Upvotes: 0

Views: 185

Answers (2)

Kastriot Dreshaj
Kastriot Dreshaj

Reputation: 1121

instead :

Typeface typeface = Typeface.createFromAsset(context.getAssets(), "fonts/redhead.ttf");
        setTypeface(typeface);

try use :

Typeface typeface = Typeface.createFromAsset(getResources().getAssets(), "fonts/redhead.ttf");
            yourTv.setTypeface(typeface);

Upvotes: 2

Anuj
Anuj

Reputation: 367

There was'nt any problem in the code,it was just that i was checking change in the xml editor rather than the device so it was not showing up.

P.S-The font change wont take place in xml editor, you will have to run it in emulator or actual device

Upvotes: 0

Related Questions