0

I want to show android application with two languages, but my application doesn't start with the language that the users selected. My default application language is "English", I want to start the application with "Burmese" Language when the users change "Language Setting". How should i do?

Julia
  • 3
  • 3
  • This thread might help : http://stackoverflow.com/questions/8049207/how-to-refresh-activity-after-changing-language-locale-inside-application – byJeevan Apr 07 '15 at 03:24
  • I have two activities "MainActivity" and "M_MainActivity". but i switch to M_MainActivity when user choose "Myanmar Language" radio button. I use simple intent. But when i quit the app and enter again, it shows "English" version not "myanmar" language. I want to switch this. In AndroidManifest.xml, i set "MainActivity" as main. Somebody help me!!! – Julia Apr 07 '15 at 04:11

3 Answers3

0

As i understand the user change the device language and you want your application to follow that language

This line will get you the device language so your app can switch between the supported languages.

Locale.getDefault().getLanguage();
Abdulrazak Alkl
  • 923
  • 8
  • 14
  • I m sorry that my question is not clear. Actually i want to say is that in my application, user can change language by choosing two radio buttons such as "Myanmar" and "English". When they change each radio i call activity respectively. The problem is that my application starts with "English" language no matter i change "Myanmar" radio. I think i write MainActivity(English version) as main activity in AndroidMenifest.xml. but i don't know how to solve it? Is there anywhere that i can change application's starting activity?? – Julia Apr 07 '15 at 04:20
0

I didn't understand the Language settings part, is it that user can change the applications language from application itself?

If No -

You can find the default language by Locale.getDefault().getLanguage();

If Yes -

Save the language that user chooses say in SharedPreference, if user had choosed English show English content else Burmese.

Yauraw Gadav
  • 1,706
  • 1
  • 18
  • 39
0

Try this...

  1. First write the Local shared preference class to store locale values.

LocalSharedManager.java

public class LocalSharedManager {

Context context;
SharedPreferences sharedPreferences;

public LocalSharedManager(Context context) {
    this.context = context;
    sharedPreferences = context.getSharedPreferences("pref",
            Context.MODE_PRIVATE);
}

public SharedPreferences getGcmPreferences() {

    return sharedPreferences;
}

public String GetValueFromSharedPrefs(String KeyName) {
    return sharedPreferences.getString(KeyName, "");
}

public String SaveValueToSharedPrefs(String KeyName, String value) {
    Editor editor = sharedPreferences.edit();
    editor.putString(KeyName, value);
    editor.commit();
    return true;
  }
 }
  1. your main activity,

MainActivity.java

  public class MainActivity extends Activity 
 {
  private RadioGroup radioGroup;
  private RadioButton english;
  private RadioButton burmese;
  private LocalSharedManager mgr;
  private Configuration config;

@Override
protected void onCreate(Bundle savedInstanceState) {
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mgr = new LocalSharedManager(MainActivity.this);
    
    radioGroup = (RadioGroup) findViewById(R.id.radioChoice);
    english=(RadioButton)findViewById(R.id.radioEnglish);
    burmese=(RadioButton)findViewById(R.id.radioBurmese);
    
    radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() 
    {

     public void onCheckedChanged(RadioGroup group, int checkedId) 
        {
         // do whatever
         if(english.isChecked())
           {
             // Do the UI changes for English.
             Toast.makeText(MainActivity.this,"English", Toast.LENGTH_SHORT).show();
             ChangeLocale();
           }
         else if(burmese.isChecked())
           {
             // Do the UI changes for Burmese.
             Toast.makeText(MainActivity.this,"Burmese", Toast.LENGTH_SHORT).show();
             ChangeLocale();
           }
      }
 });
}


/**
 * Method that use change locale type to English or Arabic.
 */
public void ChangeLocale() {
    String lang = mgr.GetValueFromSharedPrefs("App_Locale");
    if (lang.equalsIgnoreCase("")) {
        //default
        changeEnglish();
    } else {
        if (lang.equalsIgnoreCase("MY")) {
            changeBurmese();
        } else {
            changeEnglish();
        }
    }
}

public void changeBurmese() {
    new AsyncTask<Void, Void, Void>() {

        @Override
        protected Void doInBackground(Void... params) {
            String app_locale = "my";
            Locale locale = new Locale(app_locale);
            Locale.setDefault(locale);
            config = new Configuration();
            config.locale = locale;
            getResources().updateConfiguration(config,
                    getResources().getDisplayMetrics());
                
            //save selected locale in preference        
            mgr.SaveValueToSharedPrefs("App_Locale", app_locale);
            
            //refresh activity here.
            Intent i = getActivity().getIntent();
            startActivity(i);
            getActivity().finish();
            return null;
        }

    }.execute();
}

public void changeEnglish() {
    new AsyncTask<Void, Void, Void>() {

        @Override
        protected Void doInBackground(Void... params) {
            String app_locale = "en";
            Locale locale = new Locale(app_locale);
            Locale.setDefault(locale);
            config = new Configuration();
            config.locale = locale;
            getResources().updateConfiguration(config,
                    getResources().getDisplayMetrics());
                
            //save selected locale in preference        
            mgr.SaveValueToSharedPrefs("App_Locale", app_locale);
            
            //refresh activity here.
            Intent i = getActivity().getIntent();
            startActivity(i);
            getActivity().finish();
            return null;
        }

    }.execute();
  }
 }
Nimantha
  • 6,405
  • 6
  • 28
  • 69
Silambarasan Poonguti
  • 9,386
  • 4
  • 45
  • 38