简体   繁体   中英

How to change font family of Textview in whole application?

I want to change font-family of textview in whole app

Currently I'm using below code

i have added font inside my res/font folder ( Click here to check res/font directory )

Here is my font.xml files

font/font_one

<?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:font="@font/oswald_regular"
        android:fontStyle="normal"
        android:fontWeight="400"
        app:font="@font/oswald_regular"
        app:fontStyle="normal"
        app:fontWeight="400" />

    <font
        android:font="@font/oswald_bold"
        android:fontStyle="italic"
        android:fontWeight="400"
        app:font="@font/oswald_bold"
        app:fontStyle="italic"
        app:fontWeight="400" />

</font-family>

@font/font_two

<?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:font="@font/opensans_regular"
        android:fontStyle="normal"
        android:fontWeight="400"
        app:font="@font/opensans_regular"
        app:fontStyle="normal"
        app:fontWeight="400" />
    <font
        android:font="@font/opensans_bold"
        android:fontStyle="italic"
        android:fontWeight="400"
        app:font="@font/opensans_bold"
        app:fontStyle="italic"
        app:fontWeight="400" />

</font-family>

here is my xml layout

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/sampleTextView"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:layout_weight="1"
            android:background="?backColor"
            android:fontFamily="@font/font_two"
            android:padding="20dp"
            android:text="@string/app_name"
            android:textColor="?textColor"
            android:textStyle="normal" />

        <TextView
            android:id="@+id/sampleTextView2"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:layout_weight="1"
            android:background="?backColor"
            android:fontFamily="@font/font_one"
            android:padding="20dp"
            android:text="@string/app_name"
            android:textColor="?textColor"
            android:textStyle="italic" />
    </LinearLayout>


    <Spinner
        android:id="@+id/fontSpinner"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dp" />

    <Spinner
        android:id="@+id/themeSpinner"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dp" />


    <Button
        android:id="@+id/btnChaneFont"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="5dp"
        android:background="@drawable/test"
        android:text="Change Font"
        android:textColor="?textColor" />


    <Button
        android:id="@+id/btnChaneTheme"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="5dp"
        android:background="@drawable/test"
        android:text="Change Theme"
        android:textColor="?textColor" />


</LinearLayout>

here is my output when i'm using android:fontFamily its woking fine as per my requirement

OUTPUT using android:fontFamily="@font/font_one" from XML

OUTPUT using android:fontFamily="@font/font_two" from XML

here is the code of my MainActivity

public class MainActivity extends AppCompatActivity implements View.OnClickListener, AdapterView.OnItemSelectedListener {

    Spinner fontSpinner, themeSpinner;

    Button btnChaneFont, btnChaneTheme;

    TextView sampleTextView, sampleTextView2;

    int theme = R.style.AppTheme;

    String[] fontsArray = new String[]{"Oswald Fonts", "Open Sans Fonts", "Raleway Fonts"};
    String[] themeArray = new String[]{"Theme 1", "Theme 2", "Theme 3"};

    Typeface typeface = null;

    PrefManager prefManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        new ThemeColors(this);

        prefManager = new PrefManager(this);
        getTheme().applyStyle(prefManager.getTheme(), true);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        init();
    }

    private void init() {

        fontSpinner = findViewById(R.id.fontSpinner);
        themeSpinner = findViewById(R.id.themeSpinner);

        btnChaneFont = findViewById(R.id.btnChaneFont);
        btnChaneTheme = findViewById(R.id.btnChaneTheme);

        btnChaneFont.setOnClickListener(this);
        btnChaneTheme.setOnClickListener(this);

        fontSpinner.setOnItemSelectedListener(this);
        themeSpinner.setOnItemSelectedListener(this);

        sampleTextView = findViewById(R.id.sampleTextView);
        sampleTextView2 = findViewById(R.id.sampleTextView2);

        ArrayAdapter<String> fontAdapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_dropdown_item, fontsArray);
        fontSpinner.setAdapter(fontAdapter);

        ArrayAdapter<String> themeAdapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_dropdown_item, themeArray);
        themeSpinner.setAdapter(themeAdapter);

    }


    @Override
    public void onClick(View view) {

        if (view == btnChaneTheme) {
            recreate();
        }

        if (view == btnChaneFont) {

            sampleTextView.setTypeface(typeface);
            sampleTextView2.setTypeface(typeface);
        }
    }

    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int i, long l) {

        switch (parent.getId()) {
            case R.id.fontSpinner:
                switch (i) {
                    case 0:
                        typeface = ResourcesCompat.getFont(MainActivity.this, R.font.font_one);
                        break;
                    case 1:
                        typeface = ResourcesCompat.getFont(MainActivity.this, R.font.font_two);
                        break;
                    case 2:
                        typeface = ResourcesCompat.getFont(MainActivity.this, R.font.font_three);
                        break;
                }
                break;

            case R.id.themeSpinner:
                switch (i) {
                    case 0:
                        prefManager.setTheme(R.style.AppTheme);
                        break;
                    case 1:
                        prefManager.setTheme(R.style.AppTheme2);
                        break;
                    case 2:
                        prefManager.setTheme(R.style.AppTheme3);
                        break;
                }
                break;
            default:
                break;
        }
    }

    @Override
    public void onNothingSelected(AdapterView<?> adapterView) {

    }
}

Now when i'm changing the typeface of text view progrmatically using java I'm getting below result

OUTPUT using font/font_two" from JAVA

OUTPUT using font/font_two" from JAVA

Currently i'm facing below issue using above code

  • When i'm setting font-family using XML i'm getting both font effect normal and italic but when i'm setting font-family using java the italic font doesn't setting correctly (you can check the result of both i have added the screenshot)
  • how can set font-family using java
  • is there any way to set for whole application
  • How can get the same behaviour of font-family as XML using java

Can any body help me on this i want to change font-family of whole app

here are some other post that i have visited but didn't help me to solve my problem

If need more information please do let me know. Thanks in advance. Your efforts will be appreciated.

There's a library called Calligraphy https://github.com/chrisjenx/Calligraphy

Or create a custom TextView and set font at constructor In this way you should replace all the TextView in your xmls to CustomFontTextView

import android.content.Context;
import android.graphics.Typeface;
import android.support.v7.widget.AppCompatTextView;
import android.util.AttributeSet;

public class CustomFontTextView extends AppCompatTextView {

    public CustomFontTextView(Context context) {
        super(context);
        setCustomFont();
    }

    public CustomFontTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        setCustomFont();
    }

    public CustomFontTextView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        setCustomFont();
    }

    private void setCustomFont() {
        setTypeface(Typeface.create("sans-serif-light", Typeface.NORMAL));
    }

}

Typeface.create()

it's not safe approach to get a font.

Look at this answer: https://stackoverflow.com/a/16902532/10995239

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM