Skip to main content
Acrescentei comentário
Fonte Link
Murillo Comino
  • 1,1mil
  • 1
  • 8
  • 15
public class NewTextView extends AppCompatTextView {
    private Context context;
    private String fontFamily;


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

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

    public NewTextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context = context;
        init();
    }



    private void init() {
        //Eu criei sharedpreferences apenas para meu teste
        //No seu caso recupere os valores pelo seu próprio Json
        SharedPreferences preferences;
        preferences = context.getSharedPreferences(USER_FONTFAMILY, Context.MODE_PRIVATE);
        if (preferences != null) {
            fontFamily = preferences.getString(FONTFAMILY, null);

            if (fontFamily != null) {
                switch (fontFamily) {
                    case CASUAL: {
                        Typeface tf = Typeface.create("sans-serif", Typeface.NORMAL);
                        setTypeface(tf);
                        break;
                    }
                    case CURSIVA: {
                        Typeface tf = Typeface.create("serif-monospace", Typeface.NORMAL);
                        setTypeface(tf);
                        break;
                    }
                    case BASTAO: {
                        Typeface tf = Typeface.create("sans-serif-smallcaps", Typeface.NORMAL);
                        setTypeface(tf);
                        break;
                    }

                }
            }
        } else {
            //default
            Typeface tf = Typeface.create("sans-serif", Typeface.BOLD_ITALIC);
            setTypeface(tf);
        }

    }
}
public class NewTextView extends AppCompatTextView {
    private Context context;
    private String fontFamily;


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

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

    public NewTextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context = context;
        init();
    }



    private void init() {

        SharedPreferences preferences;
        preferences = context.getSharedPreferences(USER_FONTFAMILY, Context.MODE_PRIVATE);
        if (preferences != null) {
            fontFamily = preferences.getString(FONTFAMILY, null);

            if (fontFamily != null) {
                switch (fontFamily) {
                    case CASUAL: {
                        Typeface tf = Typeface.create("sans-serif", Typeface.NORMAL);
                        setTypeface(tf);
                        break;
                    }
                    case CURSIVA: {
                        Typeface tf = Typeface.create("serif-monospace", Typeface.NORMAL);
                        setTypeface(tf);
                        break;
                    }
                    case BASTAO: {
                        Typeface tf = Typeface.create("sans-serif-smallcaps", Typeface.NORMAL);
                        setTypeface(tf);
                        break;
                    }

                }
            }
        } else {
            //default
            Typeface tf = Typeface.create("sans-serif", Typeface.BOLD_ITALIC);
            setTypeface(tf);
        }

    }
}
public class NewTextView extends AppCompatTextView {
    private Context context;
    private String fontFamily;


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

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

    public NewTextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context = context;
        init();
    }



    private void init() {
        //Eu criei sharedpreferences apenas para meu teste
        //No seu caso recupere os valores pelo seu próprio Json
        SharedPreferences preferences;
        preferences = context.getSharedPreferences(USER_FONTFAMILY, Context.MODE_PRIVATE);
        if (preferences != null) {
            fontFamily = preferences.getString(FONTFAMILY, null);

            if (fontFamily != null) {
                switch (fontFamily) {
                    case CASUAL: {
                        Typeface tf = Typeface.create("sans-serif", Typeface.NORMAL);
                        setTypeface(tf);
                        break;
                    }
                    case CURSIVA: {
                        Typeface tf = Typeface.create("serif-monospace", Typeface.NORMAL);
                        setTypeface(tf);
                        break;
                    }
                    case BASTAO: {
                        Typeface tf = Typeface.create("sans-serif-smallcaps", Typeface.NORMAL);
                        setTypeface(tf);
                        break;
                    }

                }
            }
        } else {
            //default
            Typeface tf = Typeface.create("sans-serif", Typeface.BOLD_ITALIC);
            setTypeface(tf);
        }

    }
}
Fonte Link
Murillo Comino
  • 1,1mil
  • 1
  • 8
  • 15

Cria essa classe que irá extender de TextView:

public class NewTextView extends AppCompatTextView {
    private Context context;
    private String fontFamily;


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

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

    public NewTextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context = context;
        init();
    }



    private void init() {

        SharedPreferences preferences;
        preferences = context.getSharedPreferences(USER_FONTFAMILY, Context.MODE_PRIVATE);
        if (preferences != null) {
            fontFamily = preferences.getString(FONTFAMILY, null);

            if (fontFamily != null) {
                switch (fontFamily) {
                    case CASUAL: {
                        Typeface tf = Typeface.create("sans-serif", Typeface.NORMAL);
                        setTypeface(tf);
                        break;
                    }
                    case CURSIVA: {
                        Typeface tf = Typeface.create("serif-monospace", Typeface.NORMAL);
                        setTypeface(tf);
                        break;
                    }
                    case BASTAO: {
                        Typeface tf = Typeface.create("sans-serif-smallcaps", Typeface.NORMAL);
                        setTypeface(tf);
                        break;
                    }

                }
            }
        } else {
            //default
            Typeface tf = Typeface.create("sans-serif", Typeface.BOLD_ITALIC);
            setTypeface(tf);
        }

    }
}

Ao invés do SharedPreference, você recupera a configuração do usuário pelo Json, nesse meu exemplo, usei sharedpreference apenas para testar mesmo.

Nos arquivos xmls que contém os textviews, altera TODOS eles para:

        <seupacotecompleto.NewTextView
        android:layout_width="wrap_content"
        android:id="@+id/seuID"
        android:layout_height="wrap_content"
        android:text="Seu texto"/>

Caso queira ver o exemplo completo que eu fiz para testar, coloquei no meu github:

https://github.com/MurilloComino/ConfigTextviewFontFamily

Espero ter ajudado.