RTL is not working properly - android

I am currently working on an Android app that needs to support languages English and Arabic, i am using this code to switch from a language to the other.
String languageToLoad; // your language
if (languageSwitch.isChecked()) {
languageToLoad = "ar";//arabic
} else {
languageToLoad = "en";//english
}
Locale locale = new Locale(languageToLoad);
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
// store it in the cache for any further use
getBaseContext().getResources().updateConfiguration(config,
getBaseContext().getResources().getDisplayMetrics());
SharedPreferencesUtil.putString(this, "language", languageToLoad);
It was working fine, i've made 2 layouts folders, a normal one and a layout-ar. It was working correctly but sometimes it gets messed up and instead of showing the Arabic one, it shows the English layout and the app continues on running in the English mode only.

At the end you should restart you activity. Try this.
public void updateActivity() {
Intent intent = new Intent(getActivity(), MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
}

It turns out that the webview is the one responsible for the problem, once the webview is generated it deletes all the overrided local data and inserts the one from the device,in order to correct the problem you need to follow the steps found in this link.

Related

android - admob reset Locale to default values

In my app I set locale this way:
public void setLocale(String lang) {
Locale locale = new Locale(lang);
Locale.setDefault(locale);
Configuration config = getBaseContext().getResources().getConfiguration();
config.locale = locale;
getBaseContext().getResources().updateConfiguration(config,
getBaseContext().getResources().getDisplayMetrics());
}
I call this method in Activity. All work fine until I put admob banner after that all resources change to default.
I found similar issue here Admob reset app locale to system default but that solution didn't help me. What should I do to solve the problem ?
One solution that I have found is to set all resources in code depends on selected locale but it's too long for me and not convenient if you have more than two language.
Or if someone has a multi-language app with admob banner, please give me an example how do you implement localization.
helps for me:
#Override
public void onAdLoaded() {
// Code to be executed when an ad finishes loading.
setPref();
}
setPref() is method for set locale

make multi language android application

I created multi language (English, Russian, Uzbek) app. I put 4 string resoureses in 4 folders (values, values-en, values-ru, values-uz) as docs. When I change app language updates resourses configuration in App Controller like below:
Settings.LANGUAGE = prefs.getString(User.LANG, Settings.RUSSIAN);
Locale locale = new Locale(Settings.LANGUAGE);
Locale.setDefault(locale);
Configuration configuration = new Configuration();
configuration.locale = locale;
getBaseContext().getResources().updateConfiguration(configuration,
getBaseContext().getResources().getDisplayMetrics());
After that App restarts by calling App controller's method like below:
public void reStart() {
Intent i = getBaseContext().getPackageManager()
.getLaunchIntentForPackage(getBaseContext().getPackageName());
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
}
After them It works well almost all devises. But on Samsung Galaxy S6 (SM-G920F), it works as crazy. Some words are in english and others are in Uzbek and ets.
So, How to fix this error? isn't the concepts of "Supporting Different Languages" supported by (applicable to) all devices?
By the way, I have checked that all resources are given in corresponding languages (as shown in attached image):
From my observations, weird behaviour was affecting only Activity titles, and I found that I was setting translations of activity titles in Manifest file. Only these translations were misbehaving. All other dynamically set translations were working fine.
So, to fix the problem, I removed all activity labels from Manifest file, then set activity titles in onCreate method as below:
getSupportActionBar().setTitle(R.string.title_activity_followers);
Problem solved.

Android: Change to unsupported locale

I been trying to change locale using the next code:
private void changeLocal(Locale locale){
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
getBaseContext().getResources().updateConfiguration(config,
getBaseContext().getResources().getDisplayMetrics());
refresh();
}
private void refresh() {
finish();
Intent myIntent = new Intent(this, getClass());
startActivity(myIntent);
}
It works for French but not for Hebrew. So I looked in to device supported locales by calling Locale.getAvailableLocales() and I found out that French is there but Hebrew not.
If I put hardcoded Hebrew text I can see it, so it is installed, but I do not know how to force the device to use it.
Please help me force the device using Hebrew.
P.S
Wasn't been able to make this work without the refresh even when adding
android:configChanges="locale|layoutDirection"
to my Manifest it does not call the onConfigurationChanged of my Activity
Since there are 2 distinct standards, you have to double your values folder.
Add your Hebrew strings to these folders:
values-he
and
values-iw
Some devices will use one folder. Other devices will use the other one.
Note that he is actually deprecated. So, most devices will use iw

Language change on button click

stucked with one problem that in my application i have one button which is having text SPANISH on it. On the click of this button i want to change the whole application to Spanish language.
Locale mLocale = new Locale("es");
Locale.setDefault(mLocale);
Configuration config = getBaseContext().getResources().getConfiguration();
if (!config.locale.equals(mLocale))
{
config.locale = mLocale;
getBaseContext().getResources().updateConfiguration(config, null);
}
I also have created two separate Strings.xml file. but not got success.
I want to change the application language on button click.
Thanks
you have to call setContentView() after you change the locale

Localize Android application so I can switch locale inside app

How do I localize application so it uses specific locale regardless of what locale set on device? I want make it possible for users to set language of their choice.
So far I have code like this in my Application class:
#Override
public void onCreate()
{
//Set locale
String l = Preferences.getLocale(getApplicationContext());
if (!l.equals(""))
{
Locale locale = new Locale(l);
Locale.setDefault(locale);
Configuration config = getBaseContext().getResources().getConfiguration();
config.locale = locale;
getBaseContext().getResources().updateConfiguration(
config, getBaseContext().getResources().getDisplayMetrics());
}
LogData.InsertMessage(getApplicationContext(), "Application started");
}
Problem that I have is that it seems like I display in set locale just fine (TextViews)
But Menu captions and toasts will fall to default locale.
Is there any 1-2-3 on how to get it working properly? I uses 2.2 version
This post explains how to force localization in your app.
Ok, I figured why I had this problem.. I needed to override onConfigurationChanged in my application class. That is much more elegant solution than to specify locale on each Activity.

Categories

Resources