Language change on button click - android

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

Related

RTL is not working properly

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.

Android time picker dialog language changed from Locale setting

I am using two languages in app, English and Swedish.
I have used below code for managing language change by user,
final Locale locale;
if (languagecode == 1)
{
locale = new Locale("sv");
}
else
{
locale = new Locale("en");
}
Locale.setDefault(locale);
final Configuration config = new Configuration();
config.locale = locale;
mContext.getResources().updateConfiguration(config, mContext.getResources().getDisplayMetrics());
Now when I show Time picker dialog with Swedish language selected as default, it shows swedish translation in dialog, it also changes the AM/PM text. Is there any way to put custom text over there or it comes as system default? Please check the image below for reference. Thanx.
There is a tutorial here for customizing time pickers. You can define the title for example as a string resource and then create separate resource folders for translations (more info on localization here).

Runtime display image from french drawable if language changed to french

I am developing an app with english and french language support.
If user change language , textview texts are changed.
But images in ImageView does not change.
If user selects language=='fr' image should be displayed from drawable-fr.
How to achieve this?
I am changing locale like:
Locale myLocale = new Locale(language);
// set the new locale
Locale.setDefault(myLocale);
Configuration config = new Configuration();
config.locale = myLocale;
getApplicationContext().getResources().updateConfiguration(config,
getApplicationContext().getResources().getDisplayMetrics());
I solved problem.
As user changes language,i was resetting image like iv.setImageResouse(R.drawable.asdf); in onRestart() method of previous activity.
But then i replaced this with iv.setImageDrawable(getResources().getDrawable(R.drawable.asdf));
This worked.

How to change device locale language setting of device?

I want to convert my android project to multiple language. I have created a list view where different languages are going to be shown. I want to change the device language when I select any item from this list. How can I do this?
You can change the the Language like this
Locale locale = new Locale("en_US");
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
context.getApplicationContext().getResources().updateConfiguration(config, null);

EditText in Spanish

I have the following EditText:
EditText et1 = (EditText) findViewById(R.id.txtSp);
When user types in this EditText, it should display text in Spanish instead of English.
How can I achieve this?
Note: My application's language is English.
You can change the locale at runtime using that code:
Resources res = getApplicationContext().getResources();
Locale locale = new Locale("en"); //<--- use your locale code here
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
res.updateConfiguration(config, res.getDisplayMetrics());
I assume you need to reset your layout afterwards too.
According to this post, it is not possible to change the locale for a currently running app.
Google Groups -> Android Developers
The "DH" that Richard mentions in this post is an Android Framework Engineer. Look at Richard's previous post for a link to her comments.

Categories

Resources