Reputation: 47
I want to make application in two languages. When the application starts, there will be two buttons: the first button for Language X and the second for English. When I click on one of the language buttons, the application should start with the corresponding language. But I don't know how to do it. Is it possible?
Upvotes: 0
Views: 1021
Reputation: 22647
Changing the locale for your app can be done as follows:
private void setLocale(Locale locale) {
Locale.setDefault(locale);
Resources res = getContext().getResources();
DisplayMetrics dm = res.getDisplayMetrics();
android.content.res.Configuration conf = res.getConfiguration();
conf.locale = locale;
res.updateConfiguration(conf, dm);
}
On top of that, you of course need to provide localized strings.xml
at the bare minimum.
Without more details, though, I'd question why you want to do this. The user should select the language from Android's settings, and your app should follow the language of the device. This happens automatically--the system will pick the resources you provide that are qualified with a language.
I won't try to post a guide on how to provide localized resources here. Please refer to the Android documentation for details.
Upvotes: 1
Reputation: 652
If you want to have different languagas you should better try to use different strings for each language you want. For example:
values/strings.xml
values/strings-de.xml
values/strings-sk.xml
values/strings-pl.xml
Upvotes: 0
Reputation: 12453
Localization is always possible, but the same concepts apply on Android/Smartphones as they do on Computers. If the Phone itself is set to display in a certain language then your application should also display in that language. Your job as a developer is to ensure your solution and programming are Unicode compliant and handle the appropriate characters you might expect that may be unique to certain languages.
Upvotes: 0
Reputation: 3123
Here's my approach on this, to which for me easy to do.
Create a set of resources (drawables,strings,layouts,menus etc.) using language as a qualifier.
When your main activity starts with two buttons, in the call back methods of your click listeners of your buttons manipulate the locale there, I think the system will figure out what resources to use based on your qualifier which is a language.
Upvotes: 0
Reputation: 44308
when you click on one of the buttons you can set the new Locale('languageCode')
in that button's setOnClickListener()
and then reinitialize the textual elements which will re-render in the correct locale
Upvotes: 0