3

I need to change my app's font to a custom font. I'm using Android Native Fonts for this. I have added my_font.ttf and my_font_italic.ttf on the font folder.

Also, I created this font family resource

<?xml version="1.0" encoding="utf-8"?> <font-family xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto"> <font android:fontStyle="normal" android:font="@font/my_font" app:fontStyle="normal" app:font="@font/my_font"/> <font android:fontStyle="italic" android:font="@font/my_font_italic" app:fontStyle="italic" app:font="@font/my_font_italic"/> </font-family> 

On my layout, I have a LinearLayout with a couple of TextViews like this:

<LinearLayout android:id="@+id/test" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <TextView android:textAppearance="@style/text_28_italic" android:layout_width="wrap_content" android:layout_height="wrap_content" android:fontFamily="@font/my_font" android:text="Test Text"/> <TextView android:textAppearance="@style/text_28_bold" android:layout_width="wrap_content" android:layout_height="wrap_content" android:fontFamily="@font/my_font" android:text="Test Text"/> </LinearLayout> 

The styles I'm using are:

<style name="text_28_italic"> <item name="android:fontFamily">@font/my_font</item> <item name="android:textSize">28sp</item> <item name="android:textStyle">italic</item> </style> <style name="text_28_bold"> <item name="android:fontFamily">@font/my_font</item> <item name="android:textSize">28sp</item> <item name="android:textStyle">bold</item> </style> 

If I test the app, the fonts in the text views are shown correctly.

However, the problem comes when I try to add TextViews by code. According to the documentation, I need to use Typeface typeface = ResourcesCompat.getFont(context, R.font.myfont); to set the font by code. But I also need to set a style so I'm using setTextAppearance() too.

Here is my code:

TextView tv1 = new TextView(this); tv1.setText("Test text"); tv1.setTypeface(ResourcesCompat.getFont(this, R.font.my_font)); tv1.setTextAppearance(this, R.style.text_28_italic); ((LinearLayout) findViewById(R.id.test)).addView(tv1); TextView tv2 = new TextView(this); tv2.setText("Test text") tv2.setTextAppearance(this, R.style.text_28_bold); tv2.setTypeface(ResourcesCompat.getFont(this, R.font.my_font)); ((LinearLayout) findViewById(R.id.test)).addView(tv2); 

This way, I cannot make it work. I get the font applied, but not the text appearance.

How can I make both text appearance and typeface work programmatically?

Thanks!

4 Answers 4

5
Typeface typeface = ResourcesCompat.getFont(this, R.font.roboto); 

.............or........................

Typeface typeface = getResources.getFont(R.font.roboto); textView.setTypeface(typeface); 
Sign up to request clarification or add additional context in comments.

1 Comment

I am getting error at R.font. Android Studio makes it red as an error.
0

Alternatively you could use the Calligraphy Library to set custom fonts in your app via XML like this,

<TextView android:text="@string/hello_world" android:layout_width="wrap_content" android:layout_height="wrap_content" fontPath="fonts/Roboto-Bold.ttf"/> 

Custom font in TextAppearance

<style name="TextAppearance.FontPath" parent="android:TextAppearance"> <!-- Custom Attr--> <item name="fontPath">fonts/RobotoCondensed-Regular.ttf</item> </style> <TextView android:text="@string/hello_world" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="@style/TextAppearance.FontPath"/> 

Custom font in Styles

<style name="TextViewCustomFont"> <item name="fontPath">fonts/RobotoCondensed-Regular.ttf</item> </style> 

Custom font defined in Theme

<style name="AppTheme" parent="android:Theme.Holo.Light.DarkActionBar"> <item name="android:textViewStyle">@style/AppTheme.Widget.TextView</item> </style> <style name="AppTheme.Widget"/> <style name="AppTheme.Widget.TextView" parent="android:Widget.Holo.Light.TextView"> <item name="fontPath">fonts/Roboto-ThinItalic.ttf</item> </style> 

1 Comment

Thanks, but this is not a feasable alternative for me.
0

Copy your .ttf font to the asset folder. Then create typeface and apply it to your widget like below.

Typeface tf = Typeface.createFromAsset(getActivity().getAssets(),"YourFontName.ttf"); //you can use your activity name instead of getActivity(). TextView tv = (TextView) getActivity().findViewById(R.id.tv1); tv.setTypeface(tf); 

To change Appearance use below code.

tv.setTextAppearance(this, android.R.style.TextAppearance_Large); tv.setTypeface(tv.getTypeface(),Typeface.BOLD); 

1 Comment

I will try this, but this is not using Android native fonts. Is there a way for doing this using native fonts?
0

If you want to set style with custom fonts in a textview programmatically, this is how you can do it

textView?.setTextAppearance(R.style.your_style) textView?.typeface = Typeface.createFromAsset( context.assets, context.getString(R.string.your_font_path)) ) 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.