Skip to content

Instantly share code, notes, and snippets.

@Bahaaib
Created July 27, 2016 15:24
Show Gist options
  • Select an option

  • Save Bahaaib/a289c349d9dd702eb07b53e5ea3e056b to your computer and use it in GitHub Desktop.

Select an option

Save Bahaaib/a289c349d9dd702eb07b53e5ea3e056b to your computer and use it in GitHub Desktop.
package com.example.robpercival.languagesexample;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
public class MainActivity extends Activity {
TextView textView;
public void showAlert() {
final SharedPreferences sharedPreferences = this.getSharedPreferences("com.example.robpercival.languagesexample", Context.MODE_PRIVATE);
new AlertDialog.Builder(this) // construct Action Bar within (this) context
.setIcon(android.R.drawable.ic_dialog_alert) //select an icon from Android studio library to lable the dialog
.setTitle("Which Language Do You Want?") // type the dialog title
.setMessage("Do you want English or Spanish?") // type the dialog desired message
.setPositiveButton("English", new DialogInterface.OnClickListener() { // set the positive option on the dialog
@Override
public void onClick(DialogInterface dialog, int which) { // tell the system what to do in that case?
sharedPreferences.edit().putString("language", "english").apply(); // store the action into A SharedPr. to permanently appear
textView.setText("english"); // modify the TextView content
}
})
.setNegativeButton("Spanish", new DialogInterface.OnClickListener() { // set the Negative (other) option for the dialog
@Override
public void onClick(DialogInterface dialog, int which) { // tell the system what to do in that case?
sharedPreferences.edit().putString("language", "spanish").apply();
textView.setText("spanish");
}
})
.show();
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// declare all variables in background
textView = (TextView) findViewById(R.id.textView);
final SharedPreferences sharedPreferences = this.getSharedPreferences("com.example.robpercival.languagesexample", Context.MODE_PRIVATE);
String chosenLanguage = sharedPreferences.getString("language", "");
if (chosenLanguage == "") {
showAlert();
} else {
textView.setText(chosenLanguage);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.changeLanguage) {
showAlert();
return true;
}
return super.onOptionsItemSelected(item);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment