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.
Related
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;
}
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.
I'm struggling to get my android app to exit via the alertdialog.
Clicking 'yes' to play again works fine as in it restarts the app and you get to go again.
Unfortunately, when I click 'no' it just removes the dialog alert box and I can't figure out why.
Any help greatly appreciated.
Thanks
#Override
public void onClick(View v) {
AlertDialog.Builder alert = new AlertDialog.Builder(Task1Activity.this);
alert.setMessage("You have guessed incorrectly three times. " +
"The answer was " + ranNum + ". " + "Would you like to play again?")
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
Intent i = new Intent(Task1Activity.this, Task1Activity.class);
startActivity(i);
}
});
alert
.setCancelable(false)
.setNegativeButton("No", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
Task1Activity.this.finish();
};
)
.setTitle("Unlucky!")
.create();
you are creating the Activity Intent i = new Intent(Task1Activity.this, Task1Activity.class); which added the activity on stack.
try this code:
final AlertDialog.Builder alertDialog = new AlertDialog.Builder(
Task1Activity.this);
alertDialog.setTitle("Unlucky!");
alertDialog.setCancelable(false)
alertDialog.setMessage("You have guessed incorrectly three times. " +
"The answer was " + ranNum + ". " + "Would you like to play again?");
alertDialog.setPositiveButton("YES",
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
Intent i = new Intent(Task1Activity.this, Task1Activity.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
startActivity(i);
}
});
alertDialog.setNegativeButton("NO",
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
alertDialog.dismiss();
//getActivity().finish();
finishAffinity(); //finish all previous activity on stack
}
});
alertDialog.show();
You keep adding activities on top of each other every time you do:
DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
Intent i = new Intent(Task1Activity.this, Task1Activity.class);
startActivity(i);
}
});
In order to keep only 1 activity alive you should finish() the old one when you start a new activity:
DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
Intent i = new Intent(Task1Activity.this, Task1Activity.class);
startActivity(i);
finish();
}
});
I think problem is startActivity(i);
When you click on the ok, startActivity(i) starts another Task1Activity on current Task1Activity and when you press the cancel, the top Task1Activity is finishing and you go back the previous Task1Activity.
Try startActivity(i) with flag.
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
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();
I'm developing an Android application and I have this question:
How can I do to make execution waits until user has selected an option from an AlertDialog?
This is my code:
if (mPerson== null)
{
mPerson = new Person();
AlertDialog dialog = null;
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage(getString(R.string.dialog_message_select))
.setTitle(getString(R.string.dialog_title_attention));
builder.setPositiveButton(R.string.male, new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int id)
{
mPerson.setGender(Gender.male);
dialog.dismiss();
}
});
builder.setNegativeButton(R.string.female, new DialogInterface.OnClickListener()
{
#Override
public void onClick(DialogInterface dialog, int arg1)
{
mPerson.setGender(Gender.female);
dialog.dismiss();
}
});
dialog = builder.create();
dialog.show();
}
// TODO: Show data.
getWidgetsRefereces();
customizeLayout();
loadSpinnerValues();
After dialog.dismiss() I have to execute this:
// TODO: Show data.
getWidgetsRefereces();
customizeLayout();
loadSpinnerValues();
Do the following:
AlertDialog dialog = null;
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage(getString(R.string.dialog_message_select))
.setTitle(getString(R.string.dialog_title_attention));
builder.setPositiveButton(R.string.male, new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int id)
{
mPerson.setGender(Gender.male);
dialog.dismiss();
postSelection();
}
});
builder.setNegativeButton(R.string.female, new DialogInterface.OnClickListener()
{
#Override
public void onClick(DialogInterface dialog, int arg1)
{
mPerson.setGender(Gender.female);
dialog.dismiss();
postSelection();
}
});
dialog = builder.create();
dialog.show();
}
Call this method once the selection is complete.
public void postSelection(){
getWidgetsRefereces();
customizeLayout();
loadSpinnerValues();
}
Think in terms of event based execution. If you want some code to execute when you press a button, then wire it to do so. Place the code in question in a method that you can call whenever you want.
Generally, when you are programming on Android, you need to adhere to the event based nature of the platform. Traditional procedural sequential thinking will lead you to dead ends.
You have to customize your dialog interface like this .
You should use onDismissListener on dialog interface.Dont make it anonymous.
private class MyDialogInterfaceMethod implements DialogInterface.OnClickListener,DialogInterface.OnDismissListener
{
#Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
//when user click a button
}
#Override
public void onDismiss(DialogInterface dialog) {
// TODO Auto-generated method stub
//put your code here
}
}
Than use it on your alert dialog like this
builder.setNegativeButton("CANCEL",new MyDialogInterfaceMethod ());