Search code examples
androidtextcoding-stylestyles

Font gets changed in Android Studio preview but not in the device


I created a style which is applied to a TextView. It appears in Android Studio preview as I wanted to, but on the device it's different. What can I do?

This is XML code:

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
    style="@style/smalltextstyle"
        android:text="text here "
     />
</ScrollView>

This is my style code:

<style name="smalltextstyle">
    <item name="android:layout_width">match_parent</item>
    <item name="android:layout_height">match_parent</item>
    <item name="android:textAlignment" tools:targetApi="jelly_bean_mr1">center</item>
    <item name="android:textSize">23sp</item>
    <item name="android:textColor">#000</item>
    <item name="android:textStyle">bold</item>

That's what it must be

Expected appearance

This is what appears

Actual appearance


Solution

  • use this below class for your customTextview and in assets folder download your fonts

    xml

    Eg:

     <com.yourpackagename.font.MyTextView
                    android:id="@+id/text_phone_number"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="@string/text_phone_number"
                    android:textColor="@color/colorDarkAsh_opacity"
                    android:layout_marginTop="20dp"
                    android:layout_marginStart="10dp"
                    android:textSize="12sp"/> 
    

    Class

    public class MyTextView extends AppCompatTextView {
    
        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;
    
                switch (getTypeface().getStyle()) {
                    case Typeface.BOLD:
                        tf = Typeface.createFromAsset(getContext().getAssets(), "fonts/AvenirLTMedium.otf");
                        break;
    
                    case Typeface.ITALIC:
                        tf = Typeface.createFromAsset(getContext().getAssets(), "fonts/AvenirLTBookOblique.otf");
                        break;
    
                    case Typeface.BOLD_ITALIC:
                        tf = Typeface.createFromAsset(getContext().getAssets(), "fonts/AvenirLTMediumOblique.otf");
                        break;
    
                    case Typeface.NORMAL:
                        tf = Typeface.createFromAsset(getContext().getAssets(), "fonts/AvenirBook.otf");
                        break;
    
                    default:
                        tf = Typeface.createFromAsset(getContext().getAssets(), "fonts/AvenirBook.otf");
                        break;
    
                }
    
                setTypeface(tf);
            }
        }
    
    }