I have a little issue with my app. When I click on my Activity the pop-up open when I simply want it to open after we select one of the language.
Can someone help me with it and explain why it does thata and how to improve it?
Thanks a lot!
public class LocalizationUpdaterActivity extends Activity {
private String[] languages = { "English", "Francais", "Espanol", "Ivrit" };
/**
* Called when the activity is first created.
*/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_langues);
SharedPreferences sp = this.getApplicationContext().getSharedPreferences("loginSaved", Context.MODE_PRIVATE);
final SharedPreferences.Editor editor = sp.edit();
Spinner spinner = (Spinner) findViewById(R.id.spinner1);
spinner.setPrompt("select language");
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, languages);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView arg0, View arg1,
int arg2, long arg3) {
Configuration config = new Configuration();
switch (arg2) {
case 0:
config.locale = Locale.ENGLISH;
editor.putString("Langues", "en_US");
break;
case 1:
config.locale = Locale.FRENCH;
editor.putString("Langues", "fr_FR");
break;
case 2:
config.locale = new Locale("es_ES");
editor.putString("Langues", "es_ES");
break;
case 3:
config.locale = new Locale("he", "IL");
editor.putString("Langues", "he_IL");
break;
default:
config.locale = Locale.ENGLISH;
editor.putString("Langues", "en_US");
break;
}
popup("Warning !","The App will retart to apply the changes");
getResources().updateConfiguration(config, null);
}
public void onNothingSelected(AdapterView arg0) {
// TODO Auto-generated method stub
}
});
}
public void killApplication(Activity activity) {
//Broadcast the command to kill all activities
Intent intent = new Intent("kill");
intent.setType("content://all");
activity.sendBroadcast(intent);
}
public void restartApplication() {
killApplication(this);
//Start the launch activity
Intent i = this.getBaseContext().getPackageManager().getLaunchIntentForPackage(this.getBaseContext().getPackageName());
this.startActivity(i);
}
public void popup(String titre, String texte) {
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this,
AlertDialog.THEME_HOLO_DARK);
alertDialogBuilder.setTitle(titre).setMessage(texte)
.setCancelable(false)
.setNegativeButton("Ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
LocalizationUpdaterActivity.this.restartApplication();
}
});
alertDialogBuilder.create();
alertDialogBuilder.show();
}
public static void CopyStream(InputStream is, OutputStream os) {
final int buffer_size = 1024;
try {
byte[] bytes = new byte[buffer_size];
for (;;) {
int count = is.read(bytes, 0, buffer_size);
if (count == -1)
break;
os.write(bytes, 0, count);
}
} catch (Exception ex) {
}
}
}
Write setOnItemSelectedListener separately and set it in Post for spinner