-1

I have an android app and i have different activities and in that activities i have different textviews. I have two buttons of two languages one for English and another for chinese. Now i want to change all the textviews from different activities in another language as soon as user press chinese language.

Here is my one activity...

public class AndroidLocalize extends Activity {
 TextView tv;
 Button b1,b2;

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        tv = (TextView) findViewById(R.id.textView1);
        b1 = (Button) findViewById(R.id.button1);
        b1.setOnClickListener(new OnClickListener() {

            public void onClick(View arg0) {
                Locale locale = new Locale("en"); 
                Locale.setDefault(locale);
                Configuration config = new Configuration();
                config.locale = locale;
                getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());
                tv.setText(R.string.greet);
            }
        });
        b2 = (Button) findViewById(R.id.button2);
        b2.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                Locale locale = new Locale("hi"); 
                Locale.setDefault(locale);
                Configuration config = new Configuration();
                config.locale = locale;
                getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());
                tv.setText(R.string.greet);
            }
        });

    }

I am able to chnage the language on button click in the same activity now i want to chnage the language in another activity also. here is my another activity..

public class Laanguage extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.language);
    TextView tv;
    tv = (TextView) findViewById(R.id.textView2);
}
Mehdi1991
  • 91
  • 1
  • 10
  • This thread might help you: http://stackoverflow.com/questions/2264874/changing-locale-within-the-app-itself – Mike M. Jul 25 '14 at 04:39
  • I have changed the textview of my activity.But now i want to chnage the language of other textview that is in another activity.. – Mehdi1991 Jul 25 '14 at 05:01

2 Answers2

0

So for that you can use Intent's putExtra and getExtra methods in your main activity and sub activities respectively.

Srijith
  • 1,695
  • 17
  • 29
  • You can check out my updated code and how to chnage the language of another textview that is in different activity.... – Mehdi1991 Jul 25 '14 at 05:01
0

You should save the chosen language to a SharedPreference and in onCreate of every Activity, get the language out and change the TextViews accordingly.

public class Statics {
    public static final String LANGUAGES_PREFS = "MyLanguagesPrefs";
}

When the user change the language to, let's say, English:

SharedPreferences language = getSharedPreferences(Statics.LANGUAGES_PREFS, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = language.edit();
editor.putString("language", "English");

And in onCreate of every Activity:

SharedPreferences language = getSharedPreferences(Statics.LANGUAGES_PREFS, Context.MODE_PRIVATE);
String language = language.getString("language", "");
// Change the TextViews

See more about SharePreferences.

Huy Tran
  • 208
  • 1
  • 8
  • Can you please just elaborate your answer..... Actually i got it but how to get language in every activity in oncreate.... – Mehdi1991 Jul 25 '14 at 05:33