0

I want to give the Spinner to change the language of the app. I am creating a demo app, in this I have multiple strings.xml files. I have created a Spinner and added the list of languages.

On Click of a language(spinner item) I want to change the language of App.

How can it be done?

strings.xml

<string name="app_name">Multi Language App</string>
<string name="action_settings">Settings</string>

<string name="welcome">Welcome!</string>
<string name="email">Email Address</string>
<string name="password">Password</string>
<string name="login">Login</string>
<string name="signup">Don\'t have account? Sign Up</string>


<string-array name="languages">
    <item name="English"></item>
    <item name="French"></item>
    <item name="Hindi"></item>
    <item name="Japanese"></item>
</string-array>

strings.xml(de)

<string name="welcome">Willkommen!</string>
<string name="email">Email Addresse</string>
<string name="password">passowrd</string>
<string name="login">Login</string>
<string name="signup">müssen nicht angemeldet? Anmeldung</string>

strings.xml(fr)

<string name="welcome">accueil</string>
<string name="email">adresse e-mail</string>
<string name="password">mot de passe</string>
<string name="login">connexion</string>
<string name="signup">Ne pas avoir un compte? signer</string>

stringd.xml(hi)

<string name="welcome">स्वागतम</string>
<string name="email">ईमेल पता</string>
<string name="password">पासवर्ड</string>
<string name="login">लॉगिन</string>
<string name="signup">खाता नहीं है? साइन अप करें</string>

MainActivity.java

public class MainActivity extends Activity implements AdapterView.OnItemSelectedListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        getActionBar().hide();

        Spinner spinner = (Spinner) findViewById(R.id.spinnerLanguage);
// Create an ArrayAdapter using the string array and a default spinner layout
        ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
                R.array.languages, android.R.layout.simple_spinner_item);
// Specify the layout to use when the list of choices appears
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Apply the adapter to the spinner
        spinner.setAdapter(adapter);



    }
    public void onItemSelected(AdapterView<?> parent, View view,
                               int pos, long id) {
        // An item was selected. You can retrieve the selected item using
        // parent.getItemAtPosition(pos)

        if(pos == 0) {

           //what to be done here to chnage the app's language?

        }

    }

    public void onNothingSelected(AdapterView<?> parent) {
        // Another interface callback
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}

Can anyone help with this?

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Sid
  • 2,792
  • 9
  • 55
  • 111
  • I think this link will help you https://stackoverflow.com/questions/12908289/how-to-change-language-of-app-when-user-selects-language – Kinjal Dec 13 '17 at 07:46
  • possible duplicate of https://stackoverflow.com/questions/3615587/localization-android – Ramesh sambu Dec 13 '17 at 07:47
  • Possible duplicate of [How to change language of app when user selects language?](https://stackoverflow.com/questions/12908289/how-to-change-language-of-app-when-user-selects-language) – AskNilesh Dec 13 '17 at 07:48

4 Answers4

3

You need to change Locale of your Device programatically like this:

Locale locale = new Locale("hi"); // where 'hi' is Language code, set this as per your Spinner Item selected
Locale.setDefault(locale);
Configuration config = getBaseContext().getResources().getConfiguration();
config.locale = locale;
getBaseContext().getResources().updateConfiguration(config,
      getBaseContext().getResources().getDisplayMetrics());

Then Refresh Your UI, if it is necessary.

Bhoomika Patel
  • 1,895
  • 1
  • 13
  • 30
1

First add this line after findViewById.

Spinner spinner = (Spinner) findViewById(R.id.spinnerLanguage);
spinner.setOnItemSelectedListener(this);

then add below code in onItemSelected()

String lang= parent.getItemAtPosition(pos).toString();
    String languageToLoad = null;

    if(lang.equals("Hindi")){
        languageToLoad="hi";
    }else if(lang.equals("English")){
        languageToLoad="en";
    }else if(lang.equals("French")){
        languageToLoad="fr";
    }else if(lang.equals("Japanese")){
        languageToLoad="de";
    }
    if(languageToLoad!=null) {
        Locale locale = new Locale(languageToLoad);
        Locale.setDefault(locale);
        Configuration config = new Configuration();
        config.locale = locale;
        getBaseContext().getResources().updateConfiguration(config,
                getBaseContext().getResources().getDisplayMetrics());
    }

Hope it will help to you.

Dharmishtha
  • 1,313
  • 10
  • 21
0

I think this link can help you Localize the UI with Translations Editor

Littel note that the german is better :)

<string name="welcome">Willkommen!</string>
<string name="email">Email Adresse</string>
<string name="password">Password</string>
<string name="login">Login</string>
<string name="signup">Sie sind nicht angemeldet? Anmelden</string>
0

call (i.e.):

setLanguage(ApplicationHelper.Language.DE);

[...]

public void setLanguage(ApplicationHelper.Language l)
    {
        Intent intent = getIntent();
        overridePendingTransition(0, 0);
        intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
        finish();
        overridePendingTransition(0, 0);
        startActivity(intent);
    }
Alessandro Verona
  • 1,157
  • 9
  • 23