Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import android.app.Activity;
- import android.content.Intent;
- import android.content.SharedPreferences;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.AdapterView;
- import android.widget.Spinner;
- import android.widget.Toast;
- import java.util.Locale;
- public class MySettings extends Activity {
- private Spinner spinnerctrl;
- private Locale myLocale;
- String lang;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- loadLocale();
- setContentView(R.layout.activity_my_settings);
- spinnerctrl = (Spinner) findViewById(R.id.spinner);
- spinnerctrl.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
- @Override
- public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
- if (position == 1) {
- lang = "ru";
- setLocale(lang);
- } else if (position == 2) {
- lang = "en";
- setLocale(lang);
- } else {
- Toast.makeText(MySettings.this, "Choose language", Toast.LENGTH_SHORT).show();
- }
- }
- @Override
- public void onNothingSelected(AdapterView<?> parent) {
- }
- });
- }
- public void setLocale(String lang) {
- myLocale = new Locale(lang);
- saveLocale(lang);
- Locale.setDefault(myLocale);
- android.content.res.Configuration config = new android.content.res.Configuration();
- config.locale = myLocale;
- getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());
- }
- public void saveLocale(String lang)
- {
- if (lang.equalsIgnoreCase(""))
- return;
- String langPref = "Language";
- SharedPreferences prefs = getSharedPreferences("CommonPrefs", Activity.MODE_PRIVATE);
- SharedPreferences.Editor editor = prefs.edit();
- editor.putString(langPref, lang);
- editor.apply();
- }
- public void saveSettings(View view) {
- saveLocale(lang);
- Intent intent = new Intent(getApplicationContext(), MySettings.class);
- intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
- startActivity(intent);
- System.exit(1);
- }
- public void loadLocale()
- {
- String langPref = "Language";
- SharedPreferences prefs = getSharedPreferences("CommonPrefs", Activity.MODE_PRIVATE);
- String language = prefs.getString(langPref, "");
- setLocale(language);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement