10

I would like say that I have a strange problem in setting textview font. If I change the Device font style from setting-> display, then textview font style also get changed. How can i prevent this?

Here is my textview in my xml layout.

<TextView
        android:id="@+id/textView1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:background="@drawable/title_bar_bg"
        android:fontFamily="sans-serif"
        android:typeface="sans"
        android:gravity="center"
        android:text="@string/people_finder"
        android:textColor="@color/WHITE"
        android:textSize="18sp"
        android:textStyle="bold"/>

android:fontFamily="sans-serif" android:typeface="sans"

Also It is working fine in some device like Samsung Galaxy Grand, Samsung Note 2. But not working in Samsung Note 8, Samsung Note 10.1 etc.

is there any other way to do this?

Thanks

savepopulation
  • 11,736
  • 4
  • 55
  • 80
Himanshu
  • 1,066
  • 6
  • 19
  • 44

4 Answers4

6

fontFamily and typeface attributes are related to android native fonts. If you want your TextViews font to be always the same regardless the device font settings, you need to programmatically set a custom Typeface.

Typeface tf = Typeface.createFromAsset(context.getAssets(), "fontName.ttf"));
TextView textView = (TextView) findViewById(R.id.textView1);
textView.setTypeface(tf);

As a side note, Calligraphy project enables actually setting custom font directly from a layout xml.

ssantos
  • 16,001
  • 7
  • 50
  • 70
  • 1
    Thanks for your response. It works, but could you please let me know why we can't set the Native fonts forcefully? As if we use serif on monospace. then it is also working, like textview.setTypeface(Typeface.MONOSPACE, Typeface.BOLD); – Himanshu Oct 09 '13 at 10:42
  • Glad it worked :) I guess it's just a design decision, but yes, something similar happens in iOS; I also wonder why there's no straight forward way for setting custom fonts from xml layouts. – ssantos Oct 09 '13 at 11:41
  • 1
    In Android 11 we are doing this and Samsung is STILL overriding our fonts. Is there some new way this still works in Android 11? – Alex Argo Feb 09 '21 at 17:08
  • @AlexArgo did you find a solution for this? – Angel Oct 04 '21 at 06:45
0
public class CustomTextView extends TextView {

    Context context;

    public CustomTextView (Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        this.context = context;
    }

    public CustomTextView (Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
    }

    public CustomTextView (Context context) {
        super(context);
        this.context = context;
    }

    public void setTypeface(Typeface tf, int style) {
        if (style == Typeface.NORMAL) {
            super.setTypeface(Typeface.createFromAsset(getContext().getAssets(), "fonts/HelveticaNeue.ttf")/*
                                                                                                             * ,
                                                                                                             * -
                                                                                                             * 1
                                                                                                             */);
        } else if (style == Typeface.ITALIC) {
            super.setTypeface(Typeface.createFromAsset(getContext().getAssets(), "fonts/HelveticaNeueItalic.ttf")/*
                                                                                                                 * ,
                                                                                                                 * -
                                                                                                                 * 1
                                                                                                                 */);
        } else if (style == Typeface.BOLD) {
            super.setTypeface(Typeface.createFromAsset(getContext().getAssets(), "fonts/HelveticaNeueBold.ttf")/*
                                                                                                                 * ,
                                                                                                                 * -
                                                                                                                 * 1
                                                                                                                 */);
        }
    }

}

and add it in ur xml like this

    <xxx.xxxx.utils.CustomTextView 
        android:id="@+id/login_username_tv"
        android:layout_height="wrap_content"
        android:layout_width="0dp"
        android:layout_weight="0.3"
        android:text="Email"
        android:padding="5dp"
        android:textColor="#333333" 
        android:textSize="12dp"/>
DropAndTrap
  • 1,538
  • 16
  • 24
  • Hi This approach is not working when we load svg files from .ttf i.e. we have created icon.ttf file with help of icomoon tool and doing textview.setText('letter associated with icon') to show icon. There it is loading letter only in textview instead of loading icon when setting->display->text style is custom. When text-style is set to default font it is working as expected.But when text style is set custom even if we do textview.setTypeface(iconfont) it is not working. – user2425109 Jun 04 '18 at 11:11
0

This worked for me in kotlin:

textView.typeface = ResourcesCompat.getFont(this, R.font.opensans_light)
0

Try this, it worked for me...

@Override
public void onConfigurationChanged(Configuration newConfig) {
    if (newConfig.fontScale != 1)
        getResources();
    super.onConfigurationChanged(newConfig);
}

@Override
public Resources getResources() {
    Resources res = super.getResources();
    Configuration newConfig = new Configuration();
    newConfig.setToDefaults();      //Setting the default (1.0f)
    res.updateConfiguration(newConfig, res.getDisplayMetrics());
    
    return res;
}

Note: Add android:configChanges="fontScale" to the Manifest <activity>

SreeniMannem
  • 81
  • 1
  • 6