Advertisement
Guest User

Untitled

a guest
May 23rd, 2016
229
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.76 KB | None | 0 0
  1.  
  2. import android.app.Activity;
  3. import android.content.Intent;
  4. import android.content.SharedPreferences;
  5. import android.os.Bundle;
  6. import android.view.View;
  7. import android.widget.AdapterView;
  8. import android.widget.Spinner;
  9. import android.widget.Toast;
  10.  
  11. import java.util.Locale;
  12.  
  13.  
  14. public class MySettings extends Activity  {
  15.     private Spinner spinnerctrl;
  16.     private Locale myLocale;
  17.     String lang;
  18.  
  19.  
  20.     @Override
  21.     protected void onCreate(Bundle savedInstanceState) {
  22.         super.onCreate(savedInstanceState);
  23.         loadLocale();
  24.  
  25.         setContentView(R.layout.activity_my_settings);
  26.         spinnerctrl = (Spinner) findViewById(R.id.spinner);
  27.         spinnerctrl.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
  28.             @Override
  29.             public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
  30.                 if (position == 1) {
  31.                     lang = "ru";
  32.                     setLocale(lang);
  33.                 } else if (position == 2) {
  34.                     lang = "en";
  35.                     setLocale(lang);
  36.                 } else {
  37.                     Toast.makeText(MySettings.this, "Choose language", Toast.LENGTH_SHORT).show();
  38.                 }
  39.  
  40.  
  41.             }
  42.  
  43.             @Override
  44.             public void onNothingSelected(AdapterView<?> parent) {
  45.  
  46.  
  47.             }
  48.         });
  49.  
  50.     }
  51.  
  52.         public void setLocale(String lang) {
  53.  
  54.             myLocale = new Locale(lang);
  55.             saveLocale(lang);
  56.             Locale.setDefault(myLocale);
  57.             android.content.res.Configuration config = new android.content.res.Configuration();
  58.             config.locale = myLocale;
  59.             getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());
  60.  
  61.         }
  62.  
  63.     public void saveLocale(String lang)
  64.     {
  65.         if (lang.equalsIgnoreCase(""))
  66.             return;
  67.         String langPref = "Language";
  68.         SharedPreferences prefs = getSharedPreferences("CommonPrefs", Activity.MODE_PRIVATE);
  69.         SharedPreferences.Editor editor = prefs.edit();
  70.         editor.putString(langPref, lang);
  71.         editor.apply();
  72.     }
  73.  
  74.     public void saveSettings(View view) {
  75.  
  76.  
  77.         saveLocale(lang);
  78.         Intent intent = new Intent(getApplicationContext(), MySettings.class);
  79.         intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
  80.         startActivity(intent);
  81.         System.exit(1);
  82.  
  83.     }
  84.  
  85.     public void loadLocale()
  86.     {
  87.         String langPref = "Language";
  88.         SharedPreferences prefs = getSharedPreferences("CommonPrefs", Activity.MODE_PRIVATE);
  89.         String language = prefs.getString(langPref, "");
  90.         setLocale(language);
  91.     }
  92.  
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement