I'm developing an app that need to support multiple languages and if the language is RTL I have to apply a custom font. For the requirement I have created my Class that extends Application
. Everything was perfect till I got Oreo version device (Before I have Marshmellow enabled device). In Oreo if we want to change the language we have to create a custom ContextWrapper
class, here problem come in.
- To use
Calligraphy
we need toOverride
theattachBaseContext
method. And - To change language we need to
Override
theattachBaseContext
too
I tried to call super.attachBaseContext
in the Overrided
method twice One for the Calligraphy and Other for the Language code like below.
@Override
protected void attachBaseContext(Context newBase) {
// create or get your new Locale object here.
String lang = Preferences
.getSharedPreferenceString(appContext, LANGUAGE_KEY, "ar");
Context context = MyContextWrapper.wrap(newBase, lang);
super.attachBaseContext(context);
super.attachBaseContext(CalligraphyContextWrapper.wrap(newBase));
}
It give IllegalStateException as we can attach base context once.
- If I use
super.attachBaseContext(context);
the language change works But Calligraphy does not. - If I use
super.attachBaseContext(CalligraphyContextWrapper.wrap(newBase));
Calligraphy works But Language change does not.
In such case how can I make all working (Calligraphy + Multi-language). I have viewed many posts/tutorials but I'm stuck in this for three days now.
Please help me complete this. Thanks
Edit: References
- CalligraphyContextWrapper.java
- ContextWrapper Class Accepted answer in link.
Looking for a solution to be able to use custom font with Calligraphy and Change Language Functionality. Alternatively provide a way so that I may be able to change Language and Apply Font to the whole app.
Note: The solution must be compatible with API 17 to the latest 27. I'm using AppCompat
.