How to change language of app when user selects language?
It's excerpt for the webpage: http://android.programmerguru.com/android-localization-at-runtime/
It's simple to change the language of your app upon user selects it from list of languages. Have a method like below which accepts the locale as String (like 'en' for English, 'hi' for hindi), configure the locale for your App and refresh your current activity to reflect the change in language. The locale you applied will not be changed until you manually change it again.
public void setLocale(String lang) {
Locale myLocale = new Locale(lang);
Resources res = getResources();
DisplayMetrics dm = res.getDisplayMetrics();
Configuration conf = res.getConfiguration();
conf.locale = myLocale;
res.updateConfiguration(conf, dm);
Intent refresh = new Intent(this, AndroidLocalize.class);
finish();
startActivity(refresh);
}
Make sure you imported following packages:
import java.util.Locale;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.util.DisplayMetrics;
add in manifest to activity android:configChanges="locale|orientation"
How to change the language of the app when THE USER selects the language?
I would suggest to pass the language name as an intent extra in the first activity and get it in the second activity and update the language accordingly. Consider the following
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// findViewbyId here for button
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, OtherActivity.class);
intent.putExtra("lang", "fr");
startActivity(intent);
}
});
}
}
and in the other activity
public class OtherActivity extends AppCompatActivity {
@Override
private void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String lang = getIntent().getStringExtra("lang") == null ? getIntent().getStringExtra("lang") : "en";
// assuming this is the method you have to call to change the language
changeLang(en);
}
}
Hope this helps.
How to change the application language by user choice?
Although its not recommended to use separate language for your app other than the Android system's . But you can still change it .
Below is the code :
private void setLocale (String localeCode , Bundle b ){
Log.d(TAG+"set location function: "+localeCode);
locale = new Locale(localeCode);
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());
getApplicationContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());
UserDetail.this.getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());
onCreate(null);
}
Use this method call on some user trigger:
setLocale("en-us",savedInstanceStat); // for english
setLocale("ar",savedInstanceStat); // for arabic
To learn more about android locals:
http://www.icanlocalize.com/site/tutorials/android-application-localization-tutorial/
Language switching inside app android
You can extend Application class (you have to declare it in the manifest as well) and put something like this in it.
Whenever you want to change language you can then call
((App)getApplicationContext()).changeLang(lang);
from within your activity.
R.string.locale_lang is just a key which is stored in strings.xml for shared preferences
public class App extends Application {
private Locale locale = null;
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
if (locale != null) {
Locale.setDefault(locale);
Configuration config = new Configuration(newConfig);
config.locale = locale;
getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());
}
}
@Override
public void onCreate() {
super.onCreate();
SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this);
String lang = settings.getString(getString(R.string.locale_lang), "");
changeLang(lang);
}
public void changeLang(String lang) {
Configuration config = getBaseContext().getResources().getConfiguration();
if (!"".equals(lang) && !config.locale.getLanguage().equals(lang)) {
Editor ed = PreferenceManager.getDefaultSharedPreferences(this).edit();
ed.putString(getString(R.string.locale_lang), lang);
ed.commit();
locale = new Locale(lang);
Locale.setDefault(locale);
Configuration conf = new Configuration(config);
conf.locale = locale;
getBaseContext().getResources().updateConfiguration(conf, getBaseContext().getResources().getDisplayMetrics());
}
}
public String getLang(){
return PreferenceManager.getDefaultSharedPreferences(this).getString(this.getString(R.string.locale_lang), "");
}
}
Related Topics
Animate Change of View Background Color on Android
How to Develop a Soft Keyboard For Android
Self-Signed Ssl Acceptance on Android
How to Disable Copy/Paste From/To Edittext
Android: Retrieving Shared Preferences of Other Application
Android.Content.Activitynotfoundexception:
Difference Between Fragmentpageradapter and Fragmentstatepageradapter
Cursorloader Usage Without Contentprovider
How to Return a Result (Startactivityforresult) from a Tabhost Activity
How to Make a Phone Call in Android and Come Back to My Activity When the Call Is Done
Difference Between Getdefaultsharedpreferences and Getsharedpreferences
How to Get Pixel Color in Android
Android Drawing Separator/Divider Line in Layout
How to Dismiss the Dialog with Click on Outside of the Dialog
Android Transparent Status Bar and Actionbar