-3

How to change the language of the app when THE USER selects the language?

I want to do almost this: http://snowpard-android.blogspot.com.br/2013/03/programmatically-change-language-in.html?google_comment_id=z13isbsazkf3hzea504celo5oy3rjzbyevo0k

but instead of changing the language of a textView, I want to create a button with the name of the language, and when the user clicks on it, it goes to a second page already translated. I already created new values with the languages, but can't think about a code that could open another page with those strings. Could anyone help me, plssss?

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115

2 Answers2

0

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.

Omar El Halabi
  • 2,118
  • 1
  • 18
  • 26
0

use localization for achieving this on on click .

       String languageToLoad  = "hi"; // change your language her this is for hindi
  Locale locale = new Locale(languageToLoad); 
    Locale.setDefault(locale);
  Configuration config = new Configuration();
   config.locale = locale;
  getBaseContext().getResources().updateConfiguration(config, 
  getBaseContext().getResources().getDisplayMetrics());
 this.setContentView(R.layout.main);
android_jain
  • 788
  • 8
  • 19
  • I don't want to use location because the user must select the language of the app – Rafaela Lopes Apr 22 '17 at 14:05
  • So exactly what you want – android_jain Apr 22 '17 at 14:06
  • I want to give the user the option to choose the language of the app with no need to change the language of his cellphone. If I use location, the app will translate automatically (that's not what I want).My idea was creating 4 buttons (corresponding to the 4 languages) and when the user clicks on them, it goes to a second page already translated. – Rafaela Lopes Apr 22 '17 at 17:42