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?