Is It Possible To Make A Button On An Alertdialog That Doesn't Automatically Close The Dialog?
Solution 1:
So, is this possible?
Not from the standard buttons in AlertDialog
, AFAIK.
If no, what alternatives do I have?
Here are some:
- Don't offer select all/none
- Don't use
AlertDialog
, but use a dialog-themed activity - Don't use
AlertDialog
, but use some other subclass ofDialog
- Your cited option (don't use
setAdapter()
, but rather create your ownView
for the dialog contents) - Don't use a
Dialog
at all, but aListActivity
started viastartActivityForResult()
(akin to #2, just without worrying about the theme)
Solution 2:
You have to overide the onClickListener of the button. For example for the Neutral Button, you will have something like this:
AlertDialogdialog= builder.create();
dialog.setButton(AlertDialog.BUTTON_NEUTRAL, "Neutral", newDialogInterface.OnClickListener() {
@OverridepublicvoidonClick(DialogInterface dialog, int which) {
}
});
dialog.setButton(AlertDialog.BUTTON_POSITIVE, "Positive", newDialogInterface.OnClickListener() {
publicvoidonClick(DialogInterface dialog, int which) {
}
});
dialog.show();
ButtonneutralButton= dialog.getButton(DialogInterface.BUTTON_NEUTRAL);
neutralButton.setOnClickListener(newView.OnClickListener() {
@OverridepublicvoidonClick(View onClick) {
/**
*
* your code
*
*/
}
});
Solution 3:
So, is this possible?
You may try to use the following method to place your custom view on AlertDialog:
android.app.AlertDialog.Builder.setView(Viewview)
That view can contain your button. Just attach an onClickListener to the button that would operate with your list view.
Solution 4:
Have you tried to extend the AlertDialog class and have it implment OnCLickListener
publicclassMyDialogextendsAlertDialogimplementsOnClickListener {
private Button myButton;
publicvoidonCreate( Bundle savedInstanceState ) {
super.onCreate( savedInstanceState );
// use LayoutInflater to get at custom buttonLayoutInflaterlayoutInflater= (LayoutInflater)getContext().getSystemService( Context.LAYOUT_INFLATER_SERVICE );
ViewcontentView= layoutInflater.inflate( R.layout.mydialog_layout, null );
// pull button from layout, set listener
myButton = (Button)contentView.findViewById( R.id.myButtonId );
myButton.setOnClickListener( this );
setContentView( contentView );
}
publicvoidonClick( View v ) {
if ( v.getId() == R.id.myButtonId ) {
// DO your button actions.
}
}
}
Following this template, you can put any buttons you need and create your own functionality within the dialog itself. You could also create your own Button at runtime, but you'll have to do the extra work of configuring the button's text, size, icon, etc.
You can then create the dialog in your activity under the onCreateDialog() call.
protected Dialog onCreateDialog( int id ) {
MyDialogdialog=newMyDialog( this, 0 );
dialog.setOnCancelListener( this );
dialog.setOnDismissListener( this );
return dialog;
}
Hope this helps.
Solution 5:
It is possible. The solution is
Override the
dismiss
method to do nothing. This will prevent the dialog from being dismiss when any of the button is clicked. Optionally, you can also save the original dismiss.Modify your OnClickListener so that it calls the superclass of your dialog (i.e. AlertDialog) 's dismiss() (namely
super.dismiss()
) when the button(s) you want is(are) clicked.
I usually make the Dialog class the listener so I will do something like this
public class MyAlertDialog extends AlertDialog implements OnClickListener { // other methods @Override public void dismiss() { } // superclass's dismiss, might come in handy when the OnClickListener is not this dialog public void normalDismiss() { super.dismiss(); } public void onClick(DialogInterface dialog, int which) { switch (which) { case BUTTON_NEGATIVE: // handle your event super.dismiss(); break; case BUTTON_NEUTRAL: // handle your event break; case BUTTON_POSITIVE: default: // handle your event super.dismiss(); break; } } }
so that it will only dismiss the dialog when either the button NEGATIVE or POSITIVE is clicked but keep the dialog on display when the button NEUTRAL is.
Post a Comment for "Is It Possible To Make A Button On An Alertdialog That Doesn't Automatically Close The Dialog?"