0

I am developing an Android app. In my app, I am using multiple languages. Totally I am localizing. But I am having a problem with it. Currently, my app will support two languages. But my problem is I want to change the typeface for all TextView, EditText, Button and so on when the user change the language.

This is how I am setting typeface programmatically when the language change:

if(Language=="mm")
{
   Typeface tf = Typeface.createFromAsset(getAssets(),"fonts/tharlon.ttf");
   icBtnFindPlaces.setTypeface(tf);
}
else{
   Typeface tf = Typeface.createFromAsset(getAssets(),"fonts/inconsolata.ttf");
   icBtnFindPlaces.setTypeface(tf);
}

As you can see above I have do it for every views with text. This is not a good way. Why I want is I want to set typeface globally for all views, not one by one. Is it possible?

halfer
  • 19,824
  • 17
  • 99
  • 186
Wai Yan Hein
  • 13,651
  • 35
  • 180
  • 372
  • I'm guessing you could extend the default controls (`TextView` etc.) and have them listen for language changes, then update the font. It is explained [in this Q&A](http://stackoverflow.com/a/5754633/3372061) how a locale change event is caught. – Dev-iL Jun 04 '16 at 06:48
  • Even if I create listener, I have to bind it for every view. What I want is change typeface globally just once. Is that possible? – Wai Yan Hein Jun 04 '16 at 06:57
  • You would have to repeat this for every `View` **type**, not every `View`. Here's an example: extend the `TextView` class and add a broadcast receiver, then use the new type of `TextView` everywhere. You can also have some `static` helper class which handles the font change of the `View` you pass into it. I haven't tried this myself, but I think it should work. – Dev-iL Jun 04 '16 at 07:00
  • @Wai Yan Hein You can do it by making custom edittext and textview or make public Font class which can be access from activity and if you want example let me know have done both – Nisarg Jun 04 '16 at 07:23
  • Yes please example. @Nisarg – Wai Yan Hein Jun 04 '16 at 07:26
  • http://stackoverflow.com/questions/2711858/is-it-possible-to-set-font-for-entire-application – z3n105 Jun 04 '16 at 08:26
  • @WaiYanHein Sorry for geting back to you late see below answer i was going post kinda same answer and check above link it'll be helpful. – Nisarg Jun 04 '16 at 08:30

1 Answers1

0

Hi here is a good example.

If there are many TextViews whose external font need to be set, then you have to create a your own custom TextView and set the font like as below:

Add FontManager as a Singleton Java class

public class FontManager {
    public static Typeface light;
    public static Typeface medium;
    public static Typeface regular;
    private static FontManager instance;
    private AssetManager mgr;

    private FontManager(AssetManager _mgr) {
        this.mgr = _mgr;
        lght = Typeface.createFromAsset(mgr,"---.ttf");
        medium = Typeface.createFromAsset(mgr,"---.ttf");
        regular = Typeface.createFromAsset(mgr,"---.ttf");
    }

    public static FontManager init(AssetManager mgr) {
        if(instance != null)
            return instance;
        return instance = new FontManager(mgr);
    }

    public static FontManager getInstance() {
        return instance;
    }

    private String fixAssetFilename(String asset) {
        // Empty font filename?
        // Just return it. We can't help.
        if (asset.length() == 0)
            return asset;

        return asset;
    }
}

Call it in your Splash Activity as:

FontManager.init(getApplicationContext().getAssets());


public class LightTextView extends TextView {

    public LightTextView(Context context) {
        super(context);
    }

    public LightTextView(Context context, AttributeSet attrs) {
        super(context, attrs);

        if(context.getString(R.string.lang).equals("en"))
            setTypeface(FontManager.light);
        else
            setTypeface(FontManager.medium);
    }
}


Add values  --> string.xml
<string name="lang">en</string>

values-fr  --> string.xml
<string name="lang">fr</string>

<com.exa.fonttest.LightTextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="16dp"
    android:gravity="center"
    android:textSize="20sp"
    android:textColor="@color/white"
    android:text="Good luck"/>

While changing the locale automatically change the font.

Good luck.

NightFury
  • 13,436
  • 6
  • 71
  • 120
user2496783
  • 101
  • 3