I have 9 fonts inside my app because my app supports 3 languages, 3 fonts for every language like this
Arabic -> ar_light_font , ar_medium_font , ar_bold_font
English -> en_light_font , en_medium_font , en_bold_font
Spanish -> es_light_font , es_medium_font , es_bold_font
There are some text views that take a light font and there are some text views that take a medium font and there are some text views that take bold font.
For example, I have these text views
<TextView
android:id="txt1"
android:fontFamily="ar_bold_font" />
<TextView
android:id="txt2"
android:fontFamily="ar_light_font" />
<TextView
android:id="txt3"
android:fontFamily="ar_medium_font" />
If you noticed if the language of the device was Spanish then will still read from Arabic font so are there any ways to change font name from ar to es depending on the device language?
CodePudding user response:
There are methods using the Android APIs, but they don't seem very reliable.
The approach I use is to declare a string
in each strings.xml
file that tells which is the current language. Something like this:
<string name="app_lang">en-rGB</string>
(Use the convention that fits better: "en-rGB", "en-GB", "english (GB)", …)
Then, in code, have somewhere the static variables:
public final static String APP_LANG_EN_GB = "en-rGB";
// more languages…
And use it when required:
void someMethod() {
String appLang = getString(R.string.app_lang);
switch(appLang) {
case APP_LANG_EN_GB:
// Logic for english GB language
break;
case …:
// Logic for another language
}
if(appLang.equals(APP_LANG_EN_GB)) {
// Do logic for english GB language
}
}