0

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

4
  • 2
    Are you getting any error? Commented Dec 16, 2014 at 12:14
  • Nah but font is also not changing Commented Dec 16, 2014 at 12:15
  • What is problem here. Commented Dec 16, 2014 at 12:15
  • Also try putting those two lines in setTypeface method overriding it in TextView class. Commented Dec 16, 2014 at 12:21

2 Answers 2

2

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);
Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.