Android : Setting language in app - android

I want to change my app language in my app because android languages don't have all languages i want to use. So i got this to change:
String languageToLoad = "en";
Locale locale = new Locale(languageToLoad);
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
getBaseContext().getResources().updateConfiguration(config,
getBaseContext().getResources().getDisplayMetrics());
However, when user opens app and change it. Then strings which is showing at that moment still are the older language it changes just when new Activity is created.
Other problem that i should somehow save what user language was select and then change language when app is started.
So how to improve this? I want that when user select language all strings would be taken from selection languages strings.xml and how to save which language user was selected?

You can use a SharedPreference to store the user's language. Here's the documentation about it: http://developer.android.com/reference/android/content/SharedPreferences.html
About the need to reload the Activity, you may find some answers here: Android: locale(system Language) change effect my application layouts

You can create a public class (public class Share) with static members like public static String language = "en"; You can easily access the fields of this class with using the code as Share.language in whole of your project. If you want to change the language you can set Share.language = "fa";. To reload the activity with selected language, you should place the following code before any activitie's super.onCreate(saveInstanceState); :
Locale l = new Locale(Shares.language);
Locale.setDefault(l);
Configuration config = new Configuration();
config.locale = l;
context.getApplicationContext().getResources()
.updateConfiguration(config, null);
Please note that, language can be set, if you have a folder like values-language in your /res folder.
Good luck, Hossein :)

Related

how to change supportsRtl value inside the app

as i know if we set android:supportsRtl="true" in manifest the app will change it's direction based on default language of the phone ..my problem is that lot's of people in my country set their default language to English whereas they know a little about English an if they open app and see the application language is English they will definetly remove the app .. so my question is is there any way to change the value inside the app . and i give them the option to change language at the beggening of the app or in setting .. ty
you can use below code to set language for app, this is for english, replace the en with you language:
Locale locale = new Locale("en");
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());
in manifest file write android:configChanges="locale".
You can give them to choose language at any time, but remember all activities language are change except current activity, you have to restart current activity.

How to change android app language without changing phone language?

I want user to select a language inside the app. Once the language is selected, I want the strings to use the particular language.
If I change the phone language, then my app runs on the set language.
I am not able to find any way to set a language without changing the phone language. In addition, the changes should be reflected once the language is set.
Could anyone please suggest a way to do it?
Try this
public static void changeLang(Context context, String lang) {
Locale myLocale = new Locale(lang);
Locale.setDefault(myLocale);
android.content.res.Configuration config = new android.content.res.Configuration();
config.locale = myLocale;
context.getResources().updateConfiguration(config, context.getResources().getDisplayMetrics());
}
Lang parameter should be "en" for English, "it" for Italian... After that you should restart your activity/fragment

How to load alternative strings.xml from resources programmatically

I want to provide three different langauges. The default language is english so all my string values in the values folder are in enlish. I created two other folders
values-de for the german language
values-cn for the chinese language
each folder contains a strings.xml file where i defined the values in german for the de folder and in chinese for the cn folder.
My question now is: How can i load a different language programmatically because i want to provide buttons in my app interface where the user can switch the language. The settings of the device wont be editable for our users. Our users can just see the app itself and nothing else so i have to provide the language switching from within my application.
You can change the configuration at runtime
Do this in onCreate of the activity
String languageToUse = "de"; // The language you want to change
Locale locale = new Locale(languageToUse);
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
getBaseContext().getResources().updateConfiguration(config,
getBaseContext().getResources().getDisplayMetrics());

Regarding Android Application Language

My problem is that i have a Diary application in which i am adding notes by selecting a particular date from Custom Calendar View.
The problem starts when user changes his phone language from English to any other, If user has selected Japanese as his phone's new language, Month's name would be converted in Japanese language.
Now i want that if user selects any other language other than English than also Month's name should be shown in English only.
I searches a lot but could not find anything helpful.
Any positive response will be highly appreciated.
Thanks.
You should use custom calender and when you display your calendar change Locale(Language) to English programatically.
How to change Locale programatically in android.
Hope it helps you.
Edit :
You can change Language using following code :
public void changeLang(String lang)
{
if (lang.equalsIgnoreCase(""))
return;
myLocale = new Locale(lang);
saveLocale(lang);
Locale.setDefault(myLocale);
android.content.res.Configuration config = new android.content.res.Configuration();
config.locale = myLocale;
getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());
updateTexts();
}
Call this method by passing your language Like :
changeLang("en");
Note : You must define your string in res/string.xml file for english language. And you should set your string from String.xml not static.
For example for your concern Your calendar Names are set like...
yourtextview.getResources().getString(R.string.january);

Get String From Different Values Folder in Android

I wanna develop android application for different types of languages. So I have used localization for it. For that I have created different values folder like values-fr, values-ja , values-de. N also created strings.xml with static value according to that values folder. So All is good. But now my question is that I wanna change UI text according to user's selection of language. So How can I manually get particular string values from values->string.xml for particular language???
I think it may be easy, But I have no idea.
Thanks,
Locale locale = new Locale("cn");
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());
http://developer.android.com/guide/topics/resources/localization.html
You just need to get the String from the file like this way
String string = getString(R.string.hello);
Android will choose the folder based on the mobile locale. See this link for further information about Android localization.
http://developer.android.com/guide/topics/resources/localization.html
And this one for String resources if you still have some doubts on how to acces the strings.
http://developer.android.com/guide/topics/resources/string-resource.html
Hope it helps, good luck! :)

Categories

Resources