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.
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;
}
In my first class, I have this piece of code:
public void onBackPressed() {
new AlertDialog.Builder(this)
.setIcon(R.drawable.ic_launcher)
.setTitle(R.string.ALERTA1)
.setMessage(R.string.ALERTA2)
.setPositiveButton(R.string.ALERTA3, new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int which) {
finish();
}
})
.setNegativeButton(R.string.ALERTA4, null)
.show();
}
This will make an alert box when the back button is pressed, but this works in every class, I only want it to work in this one.
UPDATE:
public void onBackPressed() {
finish();
}
In every class.
Put this in your other classes:
public void onBackPressed() {
super.onBackPressed();
return;
}
That might 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();
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.
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 ());