I am validating an AlertDialog, and I would like to raise a Toast on top of the AlertDialog display.
I have this code, but the Toast is displayed on the activity
new AlertDialog.Builder(this).setTitle(R.string.contact_groups_add)
.setView(addView).setPositiveButton(R.string.ok,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int whichButton) {
if (wrapper.getTitle().length()>0)
{
processAdd(wrapper);
} else {
Toast.makeText(getApplicationContext(), "Name is required", Toast.LENGTH_SHORT).show();
}
}
}).setNegativeButton(R.string.cancel,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int whichButton) {
// ignore, just dismiss
}
}).show();
Instead of using AdvertDialog.Builder, you can create a custom dialog which will behave like a dialog, but is in fact a normal activity. Your toasts should be drawn normally on top of this.
Had this problem myself as well, when I wanted to show a validation message within a dialog.
The answer that seanhodges gave is probably the cleaner and better way. But a seperate activity wasnt practical for me, so i came up with this solution.
Anyway, you can use the AlerDialog.Builder, and show a toast.
If you override the OnClickListener of the button the you want to trigger the toast, you can show a toast on top of a dialog.
An example;
public void showToastOnDialog(final Context context) {
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle("Dialog title");
builder.setMessage("Dialog message");
builder.setPositiveButton(android.R.string.ok, new OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// Do nothing, you will be overriding this anyway
}
});
builder.setNegativeButton(android.R.string.cancel,
new OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// You can implement code here, because you wont be
// overriding this
}
});
final AlertDialog dialog = builder.create();
// Make sure you show the dialog first before overriding the
// OnClickListener
dialog.show();
// Notice that I`m not using DialogInterface.OnClicklistener but the
// View.OnClickListener
dialog.getButton(Dialog.BUTTON_POSITIVE).setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast toast = Toast.makeText(context,
"I`m a toast on top of a dialog.",
Toast.LENGTH_LONG);
toast.show();
// Because you are overriding the OnClicklistener, the
// dialog will not auto dismiss
// after clicking
dialog.dismiss();
}
});
}
Try this:
AlertDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_PHONE);
Related
I am trying to show a confirm AlertDialog before user press on back button or Activity going Pause.
I tried this code:
#Override
public void onBackPressed() {
super.onBackPressed();
AlertDialog.Builder confirmBuilder=new AlertDialog.Builder(DoExam.this);
confirmBuilder.setTitle("Confirm Exit");
confirmBuilder.setMessage("are you sure to exit form this activity");
confirmBuilder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
}
});
confirmBuilder.setNegativeButton("No", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
}
});
AlertDialog confirmDialog=confirmBuilder.create();
confirmDialog.show();
}
But it disappeared immediately at the same time the app back to previous activity.
I tried also to put the code at onPause method but I got the same problem.
Any help about show alert dialog and back to previous activity if clicked yes and keep user in the Activity if clicked no?
Because you are calling super.onBackPressed(); remove that and see what happens. the super.onBackPressed(); is meant to trigger the default action of closing the activity. you need to defer that call when the user dismisses the dialog.
Remove super.onBackPressed(); and add it in onClick(...) of your posetive Yes button of your Alertdialog..
like this
confirmBuilder.setPositiveButton("Yes", new DialogInterface.OnClickListener() { #Override public void onClick(DialogInterface dialog, int which) { super.onBackPressed();
} });
use this code
#Override
public void onBackPressed() {
AlertDialog.Builder confirmBuilder=new AlertDialog.Builder(DoExam.this);
confirmBuilder.setTitle("Confirm Exit");
confirmBuilder.setMessage("are you sure to exit form this activity");
confirmBuilder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// do any action you require on click
DoExam.super.onBackPressed();
}
});
confirmBuilder.setNegativeButton("No", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
}
});
AlertDialog confirmDialog=confirmBuilder.create();
confirmDialog.show();
}
In my application I have a button that when pressed I want it to display an Alert Dialog Box that asks if you want to continue. It will have two buttons: a "Continue" and "Do Not Continue". I am putting the method that opens up the dialog box within the method that opens the new Activity like so:
case R.id.bRegister:
try{
//the method for opening the alert box goes somewhere here but i don't know where yet.
Class ourClass = Class.forName("org.health.blablablabla.app.RegisterData");
Intent ourIntent = new Intent(MainActivity.this,ourClass);
finish();
startActivity(ourIntent);
overridePendingTransition(R.animator.fadein,R.animator.fadeout);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
This is currently what I have for the Alert Dialog Box method:
private void showWarning(){
AlertDialog.Builder warning = new AlertDialog.Builder(this);
warning.setTitle("Existing Data");
warning.setMessage("There is already existing data. If you continue all previous data will be deleted. Are you sure you want to continue?");
warning.setPositiveButton("Continue",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg0, int arg1)
{
arg0.dismiss();
}
});
warning.setNegativeButton("Do Not Continue",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// do nothing
}
});
}
My question is where to put the method in the first block of code, and how do I make it so that when the "Do Not Continue" button is pressed, the New Activity "RegisterData" doesn't open up.
you can make an YesNoSampleActivity and use AlertDialog.Builder like this:
public class YesNoSampleActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//Put up the Yes/No message box
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder
.setTitle("Erase hard drive")
.setMessage("Are you sure?")
.setIcon(android.R.drawable.ic_dialog_alert)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
//Yes button clicked, do something
Toast.makeText(YesNoSampleActivity.this, "Yes button pressed",
Toast.LENGTH_SHORT).show();
}
})
.setNegativeButton("No", null) //Do nothing on no
.show();
// Continue code after the Yes/No dialog
// ....
}
}
The code is not working. Please help me. It print the replace all string, but further code is not running.
when I debug this, there is no error in the code. It will show the code of alert box.
if(count>0)
{
System.out.println("replace all string name ");
// final Intent intent_ul=new Intent(this, UploadExcel.class);
AlertDialog.Builder alertDialogBuilder_ue = new AlertDialog.Builder(this);
alertDialogBuilder_ue.setTitle("Alert!!");
alertDialogBuilder_ue
.setMessage("Are you sure you want to Replace all the data related to this style ? ")
.setCancelable(false)
.setPositiveButton("Yes",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
mySQLiteAdapter.openToWrite();
mySQLiteAdapter.delete_style_measurement(style_no);
Log.d("","yes click");
count=0;
mySQLiteAdapter.close();
}
})
.setNegativeButton("No",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
Log.d("","No click");
count++;
dialog.cancel();
// startActivity(intent_ul);
//finish();
}
});
}
Add these lines before the end of if condition
AlertDialog alertDialog = alertDialogBuilder_ue.create();
alertDialog.show();
You need to add
alertDialogBuilder_ue.show();
in your code
Check with this code. This code working for me
Context context = CurrentActivity.this;
AlertDialog.Builder ad = new AlertDialog.Builder(context);
ad.setTitle("Application");
ad.setMessage("Do you want to proceed?");
ad.setPositiveButton("Yes", new OnClickListener()
{
public void onClick(DialogInterface dialog, int arg1)
{
}
});
ad.setNegativeButton("Cancel", new OnClickListener()
{
public void onClick(DialogInterface dialog, int arg1)
{
}
});
ad.setCancelable(false);
ad.show();
In your code adding alertDialogBuilder_ue.show(); should make the dialog appear.
By some people it has been suggested that you have to use alertDialogBuilder_ue.create(); to get a handle to the AlertDialog that you can then use the .show() method on.
Both are possibilities but you don't have to use the .create() option if you don't need a handle to the AlertDialog
Example:
System.out.println("in!");
AlertDialog.Builder dialog = new AlertDialog.Builder(this);
dialog.setMessage("test!!!");
dialog.setPositiveButton(R.string.dialog_ok,
new OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
System.out.println("Only one click!");
}
});
dialog.show();
Out:
in!
in!
Only one click!
Only one click!
Only one click!
Only one click!
Get the button (positive) and set enabled to false.
System.out.println("in!");
AlertDialog.Builder dialog = new AlertDialog.Builder(this);
dialog.setMessage("test!!!");
dialog.setPositiveButton(R.string.dialog_ok,
new OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// disable on 1st click;
final AlertDialog alertDialog = (AlertDialog)dialog;
alertDialog.getButton(DialogInterface.BUTTON_POSITIVE).setEnabled(false);
System.out.println("Only one click!");
}
});
dialog.show();
What i don't understand is why someone could click a second time on an alertdialog, because teh buttons are supposed to close the dialog after performing some action.
Why do you not close the dialog with
dialog.dismiss()
?
You need to disable it. I recommend a flag, which must be stored at the class level.
Boolean hasBeenClicked=false;
System.out.println("in!");
AlertDialog.Builder dialog = new AlertDialog.Builder(this);
dialog.setMessage("test!!!");
dialog.setPositiveButton(R.string.dialog_ok,
new OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
if (!hasBeenClicked)
{
hasBeenClicked=true;
System.out.println("Only one click!");
}
}
});
dialog.show();
My app opens a new view from the main view with:
Intent ElementsIntent = new Intent(this, ElementList.class);
startActivityForResult(ElementsIntent, 0);
which shows a list of elements and when pushing 1 of these elements a view opens up the same way as before with a new Activity. Inside this view I would like to show a AlertDialog in a button click handler, but when I call show() the app crashes.
I am pretty sure it has got somthing to do with the Context not being correct according to where I try and open the dialog, but I have tried making a static context from the main view, I have tried with element.this, which is the class connected to the activity, and I have tried getApplicationContext, and all of these result in an app crash.
I hope someone can explain what I am doing wrong.
Thanks.
Here is the AlertDialog code which crashes:
public void GoBackClickHandler(View v)
{
AlertDialog.Builder builder = new AlertDialog.Builder(ElementItem.this);
builder.setMessage("Skal ændringer i besvarelse gemmes?")
.setCancelable(false)
.setPositiveButton("Ja", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
if(inputIsValue())
{
UpdateELement task = new UpdateELement();
task.applicationContext = ElementItem.this;
task.execute(1);
}
}
})
.setNegativeButton("Nej", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
finish();
}
});
AlertDialog alert = builder.create();
alert.show();
}
If I move this code to the OnCreate, then the alert shows just fine and no app crash. It is only if I place it in a ClickHandler it crashes.
I finally found a soloution to this issue.
I had to save the context of the Activity in a variable in the onCreate method and then use this in the ClickHandler AlertDialog call, then everything works.
Hope this will be of help to someone else with this annoying problem.
public class SplashActivity extends AppCompatActivity implements DialogInterface.OnClickListener {
//Object to hold the listener instance
DialogInterface.OnClickListener listener;
#Override
protected void onCreate(Bundle savedInstanceState) {
//Assign this to listener.
listener = this;
handler.postDelayed(new Runnable() {
#Override
public void run() {
AlertDialog.Builder builder = new AlertDialog.Builder(SplashActivity.this);
builder.setTitle("Alert");
builder.setMessage("Alert Message...!");
//Here pass the listener object.
builder.setPositiveButton("OK", SplashActivity.this.listener);
builder.show();
}
});
}
#Override
public void onClick(DialogInterface dialog, int which) {
SplashActivity.this.finish();
}
}