AlertDialog not working properly - java

I am creating an AlertDialog which will ask the user to whether to delete the record or not ? so for that i have declare a global flag variable (above the onCreate() method)
private int yes;
if user press Yes then value of yes will be 1 &
if press No then value of yes will be 0
The Code of my AlertDialog is below
public int dialog()
{
AlertDialog.Builder alertDialog = new AlertDialog.Builder(DataListActivity.this);
alertDialog.setTitle("Alert");
alertDialog.setMessage("Are you sure to delete ?");
alertDialog.setPositiveButton("Yes", new DialogInterface.OnClickListener()
{
#Override
public void onClick(DialogInterface dialog, int which)
{
yes = 1;
}
});
alertDialog.setNegativeButton("No", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
yes=0;
}
});
alertDialog.show();
return yes;
}
on the basis of this yes i want to delete the record but either i press yes or no, the value of this flag int yes remains 0, See the LOGCAT
this one for press no
12-25 00:52:22.144 2133-2133/? E/Logggggggg:: 0
this one for press Yes
12-25 00:52:33.408 2133-2133/? E/Logggggggg:: 0
now i am checking the flag yes as,
int dd = dialog();
Log.e("Logggggggg: "," "+yes);
if (dd == 1)
{
Boolean r = mydb.deleteData(selections);
}
else
{
/////// do Nothing;
}
Can anyone tell me what's going wrong here..??

You cannot capture the value of yes as the return value of your method, because it has not been set yet at the time the return statement happens. Instead, just do the database cleanup directly in the onClick listeners for the yes and no buttons, e.g.
alertDialog.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// delete the record here
Boolean r = mydb.deleteData(selections);
}
});
The above should be considered as pseudo-code, because I am not familiar with the details of your code base. But the basic idea to respond the user selecting yes by directly handling that action in the onClick listener.

try this
private void dialog() {
AlertDialog.Builder alertDialog = new AlertDialog.Builder(getActivity());
alertDialog.setTitle("Alert");
alertDialog.setMessage("Are you sure to delete ?");
alertDialog.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
yes = 1;
Toast.makeText(getActivity(), String.valueOf(yes), Toast.LENGTH_SHORT).show();
}
});
alertDialog.setNegativeButton("No", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
yes =0;
Toast.makeText(getActivity(), String.valueOf(yes), Toast.LENGTH_SHORT).show();
}
});
alertDialog.show();
}
checking flag
if(yes==1){
Boolean r = mydb.deleteData(selections);
}else
{
/////// do Nothing;
}

you may just need to use onClick() signature value i.e. int which and assign it yes value like below code
public int dialog()
{
AlertDialog.Builder alertDialog = new AlertDialog.Builder(LoginActivity.this);
alertDialog.setTitle("Alert");
alertDialog.setMessage("Are you sure to delete ?");
alertDialog.setPositiveButton("Yes", new DialogInterface.OnClickListener()
{
#Override
public void onClick(DialogInterface dialog, int which)
{
which = 1;
yes = which;
Toast.makeText(LoginActivity.this,"value : "+yes,Toast.LENGTH_SHORT).show();
}
});
alertDialog.setNegativeButton("No", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
which = 0;
yes = which;
Toast.makeText(LoginActivity.this,"value : "+yes,Toast.LENGTH_SHORT).show();
}
});
alertDialog.show();
Toast.makeText(LoginActivity.this,"value : "+yes,Toast.LENGTH_SHORT).show();
return yes;
}

Related

How to check Alert Dialog visibility?

I want to check alert dialog is visible or not. In most post I saw that they used isShowing, but seems like its not describable now.
When user click info textview, I pause music. If user close alert dialog, music will be play again.
info_Button.setClickable(true);
info_Button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
mediaControl.pause();
AlertDialog.Builder playstopbutton_builder = new AlertDialog.Builder(exercise_arm_triceps_execute.this);
playstopbutton_builder.setTitle("WARNING").setMessage("Please get warm before exercising!");
playstopbutton_builder.create().show();
playstopbutton_builder.setCancelable(false);
//if alert dialog is visible keep music paused
//else if mediaControl.start();
}
});
since you have made Cancelable false, you might need to use
for positive button say like a okay
playstopbutton_builder.setPositiveButton(positiveBtnText,
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
//resume ur media player here
}
})
for negative button say like a cancel
playstopbutton_builder.setNegativeButton(negativeBtnText,
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
//resume ur media player here
}
})
so it would look like this
info_Button.setClickable(true);
info_Button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
mediaControl.pause();
AlertDialog.Builder playstopbutton_builder = new AlertDialog.Builder(exercise_arm_triceps_execute.this);
playstopbutton_builder.setTitle("WARNING").setMessage("Please get warm before exercising!");
playstopbutton_builder.create().show();
playstopbutton_builder.setCancelable(false);
playstopbutton_builder.setPositiveButton(positiveBtnText,
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
//resume ur media player here
}
});
playstopbutton_builder.setNegativeButton(negativeBtnText,
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
//resume ur media player here
}
});
}
});
You need to check dialog show or not change you code like this.
info_Button.setClickable(true);
info_Button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
mediaControl.pause();
AlertDialog.Builder playstopbutton_builder = new AlertDialog.Builder(exercise_arm_triceps_execute.this);
playstopbutton_builder.setTitle("WARNING").setMessage("Please get warm before exercising!");
playstopbutton_builder.create();
playstopbutton_builder.setCancelable(false);
//if alert dialog is visible keep music paused
//else if mediaControl.start();
if(!playstopbutton_builder.isShowing()){
//if its visibility is not showing then show here
playstopbutton_builder.show();
}else{
//do something here... if already showing
}
}
});
You may want to add an OnDismissListener to the playstopbutton_builder:
playstopbutton_builder.setOnDismissListener(new DialogInterface.OnDismissListener() {
#Override
public void onDismiss(DialogInterface dialog) {
mediaControl.start();
}
});
This way, when the user dismisses the Alert Dialog, the music will start to play again.
EDIT: if the OnDismissListener approach is not desired, maybe something like this would be better:
public void infoClickHandler(View v) {
mediaControl.pause();
AlertDialog.Builder b = new AlertDialog.Builder(this);
b.setMessage("restart the music?");
b.setPositiveButton("ok", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
mediaControl.start();
}
});
b.show();
}
EDIT 2: On the other hand, if the dialog cannot have positive or negative buttons, and you do not want to set cancellable to false, this seems to work:
public void infoClickHandler(View v) {
mediaControl.pause();
AlertDialog.Builder b = new AlertDialog.Builder(this);
b.setMessage("restart the music?");
b.setOnDismissListener(new DialogInterface.OnDismissListener() {
#Override
public void onDismiss(DialogInterface dialog) {
if (!mediaControl.isPlaying()) {
mediaControl.start();
}
}
});
b.show();
}
The OnDismissListener will be called when the user clicks outside of the dialog box.

Rename of button as user define is not working after exit from application

//Here is my method for rename of button but after exit its not working mean it's not showing rename of user why?
void changeNameOne(){
newNmOne ="";
lidOne=btn.getText().toString();
AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setTitle("Change Name");
alert.setMessage("Type in the text you want to set.");
final EditText ec = new EditText(this);
alert.setView(ec);
ec.setText(lidOne);
alert.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
newNmOne = ec.getText().toString();
btn.setText(newNmOne);
}
});
alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
btn.setText(lidOne);
}
});
alert.show();
}
Try following changes:
private static final String MYKEY = "myEditText1";
private static final String DEFAULT_TEXT = "I'm a button";
private String newNmOne = DEFAULT_TEXT;
void changeNameOne(){
lidOne=btn.getText().toString();
AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setTitle("Change Name");
alert.setMessage("Type in the text you want to set.");
final EditText ec = new EditText(this);
alert.setView(ec);
ec.setText(lidOne);
alert.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
newNmOne = ec.getText().toString();
btn.setText(newNmOne);
prefs.putString(MYKEY, newNmOne);////////////here this can be done in onpause too
}
});
alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
btn.setText(lidOne); //not needed
}
});
alert.show();
}
public void onResume(){
newNmOne = prefs.getString(MYKEY, DEFAULT_TEXT);//if not found set default text
btn.setText(newNmOne);
}
adjust for ur needs
If you need to preserve the text even after app exit, then use SharedPreferences. See https://developer.android.com/training/basics/data-storage/shared-preferences.html.

Android AlertDialog dismiss method does not work

I've created an AlertDialog, but I can't close it...
I can see it and press the buttons, but when I press the "positive button", the program shall do some tasks and then close (dismiss) the dialog. All tasks are being done, without closing the dialog, it just flickers once (it might close and then reopen, I don't know).
AlertDialog.Builder builder = new AlertDialog.Builder(myContext);
builder.setTitle("Congratulations, you won!");
builder.setMessage("Time: x seconds\nScore: xxxx\nHigh score: yyyy");
builder.setPositiveButton("Play again!",
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// Doing stuff!
circle1Paint = yellowPaint;
circle2Paint = yellowPaint;
circle3Paint = yellowPaint;
playing = true;
// Trying to close it after doing "the stuff"
dialog.dismiss();
}
});
builder.setNegativeButton("Exit",
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
System.exit(0);
}
});
builder.create.show();
Thanks in advance!
CoderOgden
Into your exit button put dilog.dissmiss() and then finish();
builder.setNegativeButton("Exit",
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
finish();
}
});
Try this code:
AlertDialog.Builder builder = new AlertDialog.Builder(myContext);
builder.setTitle("Congratulations, you won!");
builder.setMessage("Time: x seconds\nScore: xxxx\nHigh score: yyyy");
builder.setNegativeButton("Exit",
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
System.exit(0);
}
});
AlertDialog myDialog = builder.create();
builder.setPositiveButton("Play again!",
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// Doing stuff!
circle1Paint = yellowPaint;
circle2Paint = yellowPaint;
circle3Paint = yellowPaint;
playing = true;
// Trying to close it after doing "the stuff"
myDialog.dismiss();
}
});
myDialog.show();

AlertDialog will not close

I have an alert dialog, which offers the user two options: yes and no. When no is selected it the AlertDialog closes but when yes is selected it takes the user online to give them directions but when I go back to the application the AlertDialog is still visible and I can't seem to make it disappear automatically when yes is clicked. Below is my code:
AlertDialog.Builder alert_confirm = new AlertDialog.Builder(TrackingServiceActivity.this);
alert_confirm.setMessage("Are you sure you want directions to " + data.name +"! Tracking will be suspended if it has started!");
alert_confirm.setCancelable(false).
alert_confirm.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
getDirections(latitude, longitude, data.lat, data.lon);
stopTracker();
return;
}
});
alert_confirm.setNegativeButton("No",new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
return;
}
});
AlertDialog alert = alert_confirm.create();
alert.show();
use this in Positive button click listner
getDirections(latitude, longitude, data.lat, data.lon);
stopTracker();
dialog.dismiss();
use dialog.dismiss(); in Yes listener.
public void onClick(DialogInterface dialog, int which) {
getDirections(latitude, longitude, data.lat, data.lon);
stopTracker();
dialog.dismiss();
return;
}
You've missed dismiss(); call. Just do it like this :
AlertDialog.Builder alert_confirm = new AlertDialog.Builder(TrackingServiceActivity.this);
alert_confirm.setMessage("Are you sure you want directions to " + data.name +"! Tracking will be suspended if it has started!");
alert_confirm.setCancelable(false).
alert_confirm.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
getDirections(latitude, longitude, data.lat, data.lon);
stopTracker();
dismiss();
return;
}
});
alert_confirm.setNegativeButton("No",new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
return;
}
});
AlertDialog alert = alert_confirm.create();
alert.show();
Remove this line
alert_confirm.setCancelable(false);
[setCancelable:][1] Sets whether the dialog is cancelable or not. Default is true.

Referencing multiple Checkboxes in AlertDialog

Hey fellow Android Developers, Im having a issue currently with the below code. I am unable to figure out a way i can easily reference which Checkbox is clicked, Currently the code below is simply a Preference that when clicked, displays a AlertDialog with multiple Checkboxes.
The goal is do something specific when that Checkbox is checked, however i want to do something different possibly with each item.
Code
Preference checkboxalert = (Preference) findPreference("checkboxalert");
checkboxalert
.setOnPreferenceClickListener(new OnPreferenceClickListener() {
final CharSequence[] items = {" Easy "," Medium "," Hard "," Very Hard "};
final ArrayList<Integer> selectedItems=new ArrayList<Integer>();
public boolean onPreferenceClick(Preference preference) {
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle("Select The Difficulty Level");
builder.setMultiChoiceItems(items, null,
new DialogInterface.OnMultiChoiceClickListener() {
#Override
public void onClick(DialogInterface dialog, int indexSelected,
boolean isChecked) {
if (isChecked) {
//WHERE I WANT TO REFERENCE WHICH CHECKBOX IS CLICKED
selectedItems.add(indexSelected);
Log.i("Preference - Checkbox", "Something was clicked");
} else if (selectedItems.contains(indexSelected)) {
selectedItems.remove(Integer.valueOf(indexSelected));
}
}
})
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int id) {
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int id) {
}
});
dialog = builder.create();
dialog.show();
return true;
}
});
What's wrong with using the index?
#Override
public void onClick(DialogInterface dialog, int indexSelected,boolean isChecked)
{
if (isChecked) {
selectedItems.add(indexSelected);
//WHERE I WANT TO REFERENCE WHICH CHECKBOX IS CLICKED
switch (indexSelected)
{
case 0:
// do something if the first box is checked
break;
case 1:
// do something if the second box is checked
break;
...
}
}
It seems like this should work unless I am missing what you want.

Categories

Resources