mukesh mukesh - 2 months ago 18
Android Question

How to change language of app when user selects language?

I want my app to support three languages Spanish,Portuguese & English. And give option to select language in app.I have made

1) 3 drawable folders drawable-es,drawable-pt,drawable.

2) 3 values folder values-es,values-pt,values.Change String.xml values according to languages.

I have imageView to select language.When click it menu open that consists option English,Spanish,Portuguese.

I set Locale inside app on option selection by this code

public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.en:
Locale locale = new Locale("en");
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());
Toast.makeText(this, "Locale in English !", Toast.LENGTH_LONG).show();
break;

case R.id.pt:
Locale locale2 = new Locale("pt");
Locale.setDefault(locale2);
Configuration config2 = new Configuration();
config2.locale = locale2;
getBaseContext().getResources().updateConfiguration(config2, getBaseContext().getResources().getDisplayMetrics());

Toast.makeText(this, "Locale in Portugal !", Toast.LENGTH_LONG).show();
break;

case R.id.es:
Locale locale3 = new Locale("es");
Locale.setDefault(locale3);
Configuration config3 = new Configuration();
config3.locale = locale3;
getBaseContext().getResources().updateConfiguration(config3, getBaseContext().getResources().getDisplayMetrics());

Toast.makeText(this, "Locale in Spain !", Toast.LENGTH_LONG).show();
break;


}
return super.onOptionsItemSelected(item);
}


I have declare in Manifest- android:configChanges="locale"

It work but it have some issue.

Problem:-

1)When language selected, screen that consists image of language selection not change but other screens are change.

2)After orientation change app restore language according to locale of phone.

Answer

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); 
    startActivity(refresh); 
    finish();
} 

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"

Comments