1
votes

I have an android application with two language (depends on the users phone language) using values-ar ... Anyway I have a textview showing some text that it change depending on the phone language

Well I want to set two fonts to that textview one being applied if the phone language is english and the other if the phone language is arabic.

I'm already able to set one phone to that textview but I don't know how to set an alternative font like we can do in CSS "font-family: Arial,"Helvetica Neue",Helvetica,sans-serif;" if the Arial font failed to be set for the text the "Helvetica" font will be applied

To recap I have a text view that it's values language changes depending the phone language and I want to set a unique font to each of those values

1
Put your desired font in main-->assets-->font--> PASTE ALL YOUR FONT and set TypePhase based on your requirementNihal Srivastava
if you have any issue then let me know I will give you snippet of codeNihal Srivastava

1 Answers

0
votes

I think the easiest way is to make your own custom TextView. Just copy your fonts to assets folder, and swap between fonts by calling setFont1 and setFont2.

Update: The view will change its own font automatically toward your phone language. I set it to detect English/Arabic, but you can change it, or even add more languages. You can find two-letter language codes here.

public class MultiFaceTextView extends TextView {

    private static Typeface typeface1,typeface2;

    public MultiFaceTextView(Context context) {
        super(context);
        initialise(context);
    }

    public MultiFaceTextView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initialise(context);
    }

    public MultiFaceTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initialise(context);
    }


    private void initialise(Context context){
        if (typeface1==null) typeface1=Typeface.createFromAsset(context.getAssets(), "font1.ttf");
        if (typeface2==null) typeface2=Typeface.createFromAsset(context.getAssets(), "font2.ttf");
        changeTypeface(Locale.getDefault().getLanguage());
    }

    private void changeTypeface(String lang){
        switch (lang){
            case "en":this.setTypeface(typeface1);break;// English
            case "ar":this.setTypeface(typeface2);break;// Arabic
        }
    }

    public void setFont1(){changeTypeface("en");} 

    public void setFont2(){changeTypeface("ar");} 

}