I've implemented a singleChoiceItems AlertDialog which opens from a navigation menu in my android app. I'm using it to change the app theme (Light, Dark, System default).
Now I'm trying to save the current state of the AlertDialog in sharedPrefernces along with the theme that the user selects. For e.g, if I've selected a dark theme previously, I want the AlertDialog to show the same while applying the theme to the app.
Here's the code I've been using.
String selectedTheme = "Light";
public static final String SHARED_PREFS = "sharedPrefs";
SharedPreferences sharedPreferences = getSharedPreferences(SHARED_PREFS, MODE_PRIVATE);
selectedTheme = sharedPreferences.getString("str", "Light");
public void openDialog() {
String[] themes = {"Light", "Dark", "System default"};
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("Choose Theme");
builder.setSingleChoiceItems(themes, 2, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
selectedTheme = themes[which];
}
});
builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
SharedPreferences sharedPreferences = getSharedPreferences("SHARED_PREFS", MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("str", selectedTheme);
editor.apply();
if (selectedTheme.equals("Light")) {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
}
else if (selectedTheme.equals("Dark")) {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
}
else {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM);
}
dialog.dismiss();
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
builder.show();
}
With the above code, the sharedpreferences is not storing the current state. What changes do I need to do in order to make it work?
I was able to solve the problem by sharing the string related to the theme in sharedPreference.
I also implemented the AlertDialog issue where I wanted to check the items which was previously chosen inside the app.
Following is the code I used:
String selectedTheme = "Light";
int checkedItem = 0;
public static final String SHARED_PREFS = "sharedPrefs";
public static final String TEXT = "text";
public static final String RADIO = "selected";
Then I called loadData() and updateViews() from onCreate().
public void openDialog() {
String[] themes = {"Light", "Dark", "System default"};
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("Choose Theme");
if (selectedTheme.equals("Light")) {
checkedItem = 0;
} else if (selectedTheme.equals("Dark")) {
checkedItem = 1;
} else {
checkedItem = 2;
}
builder.setSingleChoiceItems(themes, checkedItem, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
selectedTheme = themes[which];
}
});
builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
if (selectedTheme.equals("Light")) {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
}
else if (selectedTheme.equals("Dark")) {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
}
else {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM);
}
saveData();
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
builder.show();
}
public void saveData() {
SharedPreferences sharedPreferences = getSharedPreferences(SHARED_PREFS, MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString(TEXT, selectedTheme);
editor.putInt("selected", checkedItem);
editor.apply();
Toast.makeText(MainActivity.this, "Data Saved", Toast.LENGTH_SHORT).show();
}
public void loadData() {
SharedPreferences sharedPreferences = getSharedPreferences(SHARED_PREFS, MODE_PRIVATE);
selectedTheme = sharedPreferences.getString(TEXT, "System default");
checkedItem = sharedPreferences.getInt("selected", 0);
}
public void updateViews() {
if (selectedTheme.equals("Light")) {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
checkedItem = 0;
}
else if (selectedTheme.equals("Dark")) {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
checkedItem = 1;
}
else {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM);
checkedItem = 2;
}
}
I followed the tutorial from this link.
I want to display alertDialog only once. The first time user clicks on the play button, alertDialog should appear. User can stop the game. If he wishes to play again,the alertDialog should not appear again.
I've tried using sharedPreferences, but alertDialog is never displayed.
public boolean added = false;
private void startRoute(MenuItem item) {
SharedPreferences prefs = getContext().getSharedPreferences("myPref",Context.MODE_PRIVATE);
boolean added = prefs.getBoolean("added",false);
if(!added)
{
addRouteToCal();
}
refreshProgress();
}
private void addRouteToCal() {
final EditText taskEditText = new EditText(getContext());
new AlertDialog.Builder(getContext())
.setTitle(getString(R.string.current_add_to_favorites))
.setMessage(getString(R.string.current_name_favorites))
.setView(taskEditText)
.setPositiveButton(getString(R.string.ok), new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
String name=String.valueOf(taskEditText.getText());
saveRouteDone(name);
SharedPreferences.Editor prefs = getContext().getSharedPreferences("myPref",Context.MODE_PRIVATE).edit();
prefs.putBoolean("added",true);
prefs.commit();
}
})
.setNegativeButton(getString(R.string.cancel), (dialog, which) -> {
})
.show();
;
Any idea of what I did wrong ?
My question is i have a list of food item. when user press add button to add the dish i am saving this dish name in shared preference. but when i press the same dish twice in shared preference should show 2 dishes with the same name. but each time i press same dish its showing me only one dish. this is my code below.
public class Cafetaria extends AppCompatActivity {
String title;
ListView listView;
View customNav;
public String value,secdish,thrDish,frthDish;
public String Drink,Drink2,Drink3,Drink4;
String selectedDrink;
Dialog ViewDialog;
TextView tv_foodtype,tv_drink;
private Button button;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_cafetaria);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
title = getIntent().getStringExtra("option");
getSupportActionBar().setTitle(title);
ActionBar actionBar = getSupportActionBar();
ActionBar.LayoutParams lp = new ActionBar.LayoutParams(ActionBar.LayoutParams.WRAP_CONTENT, ActionBar.LayoutParams.WRAP_CONTENT, Gravity.RIGHT | Gravity.CENTER_VERTICAL);
customNav = LayoutInflater.from(this).inflate(R.layout.food_actionbar_layout, null); // layout which contains your button.
actionBar.setCustomView(customNav, lp);
actionBar.setDisplayShowCustomEnabled(true);
customNav.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
ViewDialog = new Dialog(Cafetaria.this);
ViewDialog.setContentView(R.layout.activity_order_detail);
ViewDialog.setTitle("Your Order Details");
tv_foodtype = (TextView)ViewDialog.findViewById(R.id.nameuser);
tv_drink = (TextView)ViewDialog.findViewById(R.id.passnum);
button = (Button)ViewDialog.findViewById(R.id.okBtn);
ViewDialog.show();
final SharedPreferences mSharedPreference= PreferenceManager.getDefaultSharedPreferences(getBaseContext());
value=(mSharedPreference.getString("firstDish", ""));
Drink=(mSharedPreference.getString("selectedDrinks", ""));
Drink2=(mSharedPreference.getString("selectedDrinks1", ""));
Drink3=(mSharedPreference.getString("selectedDrinks2", ""));
Drink4=(mSharedPreference.getString("selectedDrinks3", ""));
secdish=(mSharedPreference.getString("secdish", ""));
thrDish=(mSharedPreference.getString("thirdDish", ""));
frthDish=(mSharedPreference.getString("fourtDish", ""));
tv_foodtype.setText("Main Dishes"+ " \n"+value + " \n" + secdish +"\n"+thrDish+"\n"+frthDish);
tv_drink.setText("Drink"+" \n"+Drink + " \n" + Drink2 +"\n"+Drink3+"\n"+Drink4);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
ViewDialog.dismiss();
}
});
}
});
listView =(ListView)findViewById(R.id.listView);
Drawable chic = this.getResources().getDrawable(R.drawable.chicktikka);
final Drawable plus = this.getResources().getDrawable(R.drawable.plus);
Drawable minus = this.getResources().getDrawable(R.drawable.minus);
ArrayList<FoodItemData> listofItem = new ArrayList<>();
FoodListViewAdapter listViewAdapter= new FoodListViewAdapter(this,R.layout.item_layout,listofItem);
listView.setAdapter(listViewAdapter);
listofItem.add(new FoodItemData("Chicken Tikka","spicey chicken tikka with mixures of indian spices",chic,plus,minus));
listofItem.add(new FoodItemData("Onion Bhaji","spicey chicken tikka with mixures of indian spices",chic,plus,minus));
listofItem.add(new FoodItemData("Chicken Pizza","spicey chicken tikka with mixures of indian spices",chic,plus,minus));
listofItem.add(new FoodItemData("Chicken Masala","spicey chicken tikka with mixures of indian spices",chic,plus,minus));
}
#RequiresApi(api = Build.VERSION_CODES.HONEYCOMB_MR1)
public void plusClick(View v)
{
if (listView.getPositionForView(v)==0) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(Cafetaria.this);
final SharedPreferences.Editor editor = prefs.edit();
editor.putString("firstDish","Chicken Tikka");
AlertDialog.Builder adb = new AlertDialog.Builder(this);
adb.setTitle("Please Choose your drink");
final String[] drinks = new String[]{"Coke", "Fanta", "Sprite"};
final ArrayList<Integer> selectedItems = new ArrayList<Integer>();
final boolean[] preCheckedItems = new boolean[]{false, false, false};
adb.setMultiChoiceItems(drinks, preCheckedItems, new DialogInterface.OnMultiChoiceClickListener() {
#Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
if (isChecked) {
selectedItems.add(which);
} else if (selectedItems.contains(which)) {
selectedItems.remove(which);
}
}
});
//Define the AlertDialog positive/ok/yes button
adb.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
for (int i = 0; i < selectedItems.size(); i++) {
int IndexOfColorsArray = selectedItems.get(i);
selectedDrink = Arrays.asList(drinks).get(IndexOfColorsArray);
editor.putString("selectedDrinks",selectedDrink);
editor.commit();
}
Toast.makeText(Cafetaria.this, "Your item has been added", Toast.LENGTH_SHORT).show();
}
});
//Define the Neutral/Cancel button in AlertDialog
adb.setNeutralButton("Cancel", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
}
});
adb.show();
}
else if (listView.getPositionForView(v) == 1) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(Cafetaria.this);
final SharedPreferences.Editor editor = prefs.edit();
editor.putString("secdish","Onion Bhaji");
AlertDialog.Builder adb = new AlertDialog.Builder(this);
adb.setTitle("Choose your Drink");
final String[] drinks = new String[]{"Coke", "Fanta", "Sprite"};
final ArrayList<Integer> selectedItems = new ArrayList<Integer>();
final boolean[] preCheckedItems = new boolean[]{false, false, false};
adb.setMultiChoiceItems(drinks, preCheckedItems, new DialogInterface.OnMultiChoiceClickListener() {
#Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
if (isChecked) {
selectedItems.add(which);
} else if (selectedItems.contains(which)) {
selectedItems.remove(which);
}
}
});
//Define the AlertDialog positive/ok/yes button
adb.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
for (int i = 0; i < selectedItems.size(); i++) {
int IndexOfColorsArray = selectedItems.get(i);
selectedDrink = Arrays.asList(drinks).get(IndexOfColorsArray);
editor.putString("selectedDrinks1",selectedDrink);
editor.commit();
}
Toast.makeText(Cafetaria.this, "Your item has been added", Toast.LENGTH_SHORT).show();
}
});
adb.setNeutralButton("Cancel", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
//When user click the neutral/cancel button from alert dialog
}
});
adb.show();
}
else if (listView.getPositionForView(v) == 2) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(Cafetaria.this);
final SharedPreferences.Editor editor = prefs.edit();
editor.putString("thirdDish","Chicken Pizza");
AlertDialog.Builder adb = new AlertDialog.Builder(this);
adb.setTitle("Choose your Drink");
final String[] drinks = new String[]{"Coke", "Fanta", "Sprite"};
final ArrayList<Integer> selectedItems = new ArrayList<Integer>();
final boolean[] preCheckedItems = new boolean[]{false, false, false};
adb.setMultiChoiceItems(drinks, preCheckedItems, new DialogInterface.OnMultiChoiceClickListener() {
#Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
if (isChecked) {
selectedItems.add(which);
} else if (selectedItems.contains(which)) {
selectedItems.remove(which);
}
}
});
adb.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
for (int i = 0; i < selectedItems.size(); i++) {
int IndexOfColorsArray = selectedItems.get(i);
selectedDrink = Arrays.asList(drinks).get(IndexOfColorsArray);
editor.putString("selectedDrinks2",selectedDrink);
editor.commit();
}
Toast.makeText(Cafetaria.this, "Your item has beed added", Toast.LENGTH_SHORT).show();
}
});
adb.setNeutralButton("Cancel", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
//When user click the neutral/cancel button from alert dialog
}
});
adb.show();
}
else if (listView.getPositionForView(v) == 3) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(Cafetaria.this);
final SharedPreferences.Editor editor = prefs.edit();
editor.putString("fourtDish","Chicken Masala");
AlertDialog.Builder adb = new AlertDialog.Builder(this);
adb.setTitle("Choose your Drink");
final String[] drinks = new String[]{"Coke", "Fanta", "Sprite"};
final ArrayList<Integer> selectedItems = new ArrayList<Integer>();
final boolean[] preCheckedItems = new boolean[]{false, false, false};
adb.setMultiChoiceItems(drinks, preCheckedItems, new DialogInterface.OnMultiChoiceClickListener() {
#Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
if (isChecked) {
selectedItems.add(which);
} else if (selectedItems.contains(which)) {
//selectedItems.remove(which);
selectedItems.add(which);
}
}
});
adb.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
for (int i = 0; i < selectedItems.size(); i++) {
int IndexOfColorsArray = selectedItems.get(i);
selectedDrink = Arrays.asList(drinks).get(IndexOfColorsArray);
editor.putString("selectedDrinks3",selectedDrink);
editor.commit();
}
Toast.makeText(Cafetaria.this, "Your item has beed added", Toast.LENGTH_SHORT).show();
}
});
adb.setNeutralButton("Cancel", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
//When user click the neutral/cancel button from alert dialog
}
});
adb.show();
}
}
public void minusClick(View v)
{
if (listView.getPositionForView(v)==0) {
final SharedPreferences mSharedPreference= PreferenceManager.getDefaultSharedPreferences(getBaseContext());
mSharedPreference.edit().remove("firstDish").commit();
mSharedPreference.edit().remove("selectedDrinks").commit();
Toast.makeText(Cafetaria.this, "Your Item has been removed", Toast.LENGTH_SHORT).show();
}
else if (listView.getPositionForView(v)==1)
{
final SharedPreferences mSharedPreference= PreferenceManager.getDefaultSharedPreferences(getBaseContext());
mSharedPreference.edit().remove("secDish").commit();
mSharedPreference.edit().remove("selectedDrinks1").commit();
Toast.makeText(Cafetaria.this, "Your Item has been removed", Toast.LENGTH_SHORT).show();
}
else if (listView.getPositionForView(v)==2)
{
final SharedPreferences mSharedPreference= PreferenceManager.getDefaultSharedPreferences(getBaseContext());
mSharedPreference.edit().remove("thirdDish").commit();
mSharedPreference.edit().remove("selectedDrinks2").commit();
Toast.makeText(Cafetaria.this, "Your Item has been removed", Toast.LENGTH_SHORT).show();
}
else if (listView.getPositionForView(v)==3)
{
final SharedPreferences mSharedPreference= PreferenceManager.getDefaultSharedPreferences(getBaseContext());
mSharedPreference.edit().remove("fourtDish").commit();
mSharedPreference.edit().remove("selectedDrinks3").commit();
Toast.makeText(Cafetaria.this, "Your Item has been removed", Toast.LENGTH_SHORT).show();
}
}
#Override
protected void onResume() {
super.onResume();
if (getSupportActionBar() != null){
getSupportActionBar().setTitle(title);
}
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
this.finish();
return true;
}
return false;
}
#Override
protected void onRestart() {
super.onRestart();
final SharedPreferences mSharedPreference= PreferenceManager.getDefaultSharedPreferences(getBaseContext());
mSharedPreference.edit().remove("firstDish").commit();
mSharedPreference.edit().remove("selectedDrinks").commit();
mSharedPreference.edit().remove("selectedDrinks1").commit();
mSharedPreference.edit().remove("selectedDrinks2").commit();
mSharedPreference.edit().remove("selectedDrinks3").commit();
mSharedPreference.edit().remove("secdish").commit();
mSharedPreference.edit().remove("thirdDish").commit();
mSharedPreference.edit().remove("fourtDish").commit();
}
}`
This is the result I am getting. I press this dish twice and its shown only once. It should be shown twice the same dish name. Please Guide.
The code is really long to follow on here. But speaking generally, do you save each dish with different key-value pair? Maybe think about such solution. OR you could store the dishes in an array, which can be stored in Shared prefs, and once you want to see your dishes, just read out the Array List and display the items inside it.
You store the String into an Array, whenever you want to do it in your code. It's just like mArrayList.add("String"); and then you can update yuor SharedPrefs the same you already do, just check the URL on how to store ArrayList to SharedPrefs. Would be a cleaner solution in my view.
Here you can see how to store ArrayList in SharedPreferences:
Save ArrayList to SharedPreferences
And the once you want to display the items from ArrayList, you pull ArrayList from SharedPrefs and display them in AlertDialog ListView.
And here it is explained how to display Array Items in AlertDialog:
How can I display a list view in an Android Alert Dialog?
I have a button that open a dialog for change a number. The button is in activity, and generate a new class names dialogs, for storage different dialogs.
Dialog consigna: Dialogs.class
public String consigna(){
AlertDialog.Builder alert = new AlertDialog.Builder(ctxt);
alert.setTitle("Nueva temperatura");
alert.setMessage("Agrega una nueva temperatura");
final EditText input = new EditText(ctxt);
input.setInputType(InputType.TYPE_CLASS_NUMBER);
input.setRawInputType(InputType.TYPE_CLASS_NUMBER);
alert.setView(input);
alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
data = input.getText().toString();
}
});
alert.setNegativeButton("cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
//Put actions for CANCEL button here, or leave in blank
}
});
alert.show();
return data;
}
on click button: MainActivity
//I need change these two values:
final public double temperatura = 200.3;
String newData;
...
public void onClick(View v) {
switch (v.getId()){
case R.id.tempConsigna:
dialog = new Dialogs(MainActivity.this);
String data = dialog.consigna();
newData = data;
break;
...//other cases...
The issue is in newData = data; I doesn't have a data, because the dialog not are closed. The dialog work in other thread,no?
How to change newData var with the dialog result? It is posible into a dialogs class?
You need a callback method implemented via an interface:
public String consigna(final OnConfirm confirm){
final String[] data = new String[1];
AlertDialog.Builder alert = new AlertDialog.Builder(activity);
alert.setTitle("Nueva temperatura");
alert.setMessage("Agrega una nueva temperatura");
final EditText input = new EditText(activity);
input.setInputType(InputType.TYPE_CLASS_NUMBER);
input.setRawInputType(InputType.TYPE_CLASS_NUMBER);
alert.setView(input);
alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
data[0] = input.getText().toString();
confirm.onConfirm(data[0]);
}
});
alert.setNegativeButton("cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
//Put actions for CANCEL button here, or leave in blank
}
});
alert.show();
return data[0];
}
public interface OnConfirm {
void onConfirm(String s);
}
And in your Main Activity
Dialogs dialog = new Dialogs(MainActivity.this);
dialog.consigna(new Dialogs.OnConfirm() {
#Override
public void onConfirm(String s) {
Log.d("data", s);
}
});
It will not return result as you want but after click on dialog ok button then access data variable. no need to return.
So I have some code that sets an onClickListener for a button that doesn't seem to work on devices with screen sizes less than 4 inches, but only for specific buttons. I'm not sure why, because it doesn't seem to be an effect OS Level version, but only screen size.
I have logging code in the onclick method that shows all the buttons registering and firing correctly except the new_game button. Any input on why this might be happening would be appreciated.
Code from OnCreate:
Button acknowledgements = (Button) findViewById(R.id.acknolwedgments_word_Game);
acknowledgements.setOnClickListener(this);
Button quit = (Button) findViewById(R.id.quit_word_game_button);
quit.setOnClickListener(this);
Button new_game = (Button) findViewById(R.id.word_game_new_Button);
Log.e("NEW GAME BUTTON", String.valueOf(new_game));
new_game.setOnClickListener(this);
Log.e("SET ONCLICK", "DONE");
OnClickListener:
public void onClick(View view) {
int id = view.getId();
Log.e("CLICKED BUTTON", String.valueOf(view));
if (id == R.id.quit_word_game_button){
Intent i = new Intent(this, Game.class);
startActivity(i);
}
else if (id == R.id.acknolwedgments_word_Game){
Intent i = new Intent(this, Acknowledgements.class);
startActivity(i);
}
else if (id == R.id.word_game_new_Button){
final AlertDialog alert = new AlertDialog.Builder(word_game_mainscreen.this).create();
final EditText edit = new EditText(getBaseContext());
edit.setHint("Username");
alert.setView(edit);
alert.setButton(DialogInterface.BUTTON_NEGATIVE, "CANCEL", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
alert.dismiss();
}
});
alert.setButton(DialogInterface.BUTTON_POSITIVE, "PLAY!", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
final String opponent = String.valueOf(edit.getText());
new AsyncTask(){
#Override
protected Object doInBackground(Object[] objects) {
//Code to synchronize it to a server
}
#Override
protected void onPostExecute(Object o) {
super.onPostExecute(o);
Log.e("POST EXECUTE", (String)o);
//Creates intent to take you to the game
}
}.execute();
}
});
alert.show();
}
else if (id == R.id.togglesound){
ToggleButton music = (ToggleButton) findViewById(R.id.togglesound);
if (music.isChecked()){
Music.play(this, R.raw.wordgame);
}
else{
Music.stop(this);
}
}
}
Simple suggestion: swith case is far better than if-else and class_name.this is mostly friendly than getBaseContext().
For showing AlertDialog we have to first create the builder.Something like:
AlertDialog dialog;
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setIcon(...).setTitle(...)
.setView(...)
.setPositiviButton(...)
.setNegativeButton(...);
//Now create the builder and assign to AlertDialog
dialog = builder.create();
dialog.show;