I'd like to close a dialog window in my android app by simply touching the screen.. is this possible? If so, how?
I've looked into setting some "onClickEven" on the dialog, but it doesnt exist.
How would this be possible?
You can use dialog.setCanceledOnTouchOutside(true); which will close the dialog if you touch u=outside the dialog.
If your dialog contains any view try to get the touch events in that view and dismiss your dialog when user touch on that view. For example if your dialog has any Image then your code should be like this.
Dialog dialog = new Dialog(context);
dialog.setContentView(R.layout.mylayout);
//create a layout with imageview
ImageView image = (ImageView) dialog.findViewById(R.id.image);
image.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();
Dialog dialog = new Dialog(context)
{
public boolean dispatchTouchEvent(MotionEvent event)
{
dialog.dismiss();
return false;
}
};
And you are done!
You can extend Dialog class and override dispatchTouchEvent() method.
EDIT: Also you can implement Window.Callback interface and set it as dialog's window callback using dialog.getWindow().setCallback(). This implementation should call corresponding dialog's methods or handle events in its own way.
If someone still searching for a solution to dismiss a Dialog by
onTouch Event, here is a snippet of code:
public void onClick(View v) {
AlertDialog dialog = new AlertDialog(MyActivity.this){
#Override
public boolean dispatchTouchEvent(MotionEvent event)
{
dismiss();
return false;
}
};
dialog.setIcon(R.drawable.MyIcon);
dialog.setTitle("MyTitle");
dialog.setMessage("MyMessage");
dialog.setCanceledOnTouchOutside(true);
dialog.show();
}
Related
I am showing two dialogs each should be displayed with different network call on the same activity (Login Activity). In which if I click on "Resend Email" text view in the first dialog then I am having another network call that shows me another dialog. When I click "OK" on the second dialog, it is dismissed. But the first one is still shown. So how to dismiss both when I click "Ok" on the second.
create a local Dialog variable dialogOne and when you click on ok of the dialog two dismiss both
btn_ok.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
isShown = true;
dialog.dismiss();
dialogOne.dismiss();
}
});
Dismiss both dialog when button clicked and before dismiss must check dialog is visible or not to avoid nullpointer exception.
btn_ok.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(dialog.isShowing())
dialog.dismiss();
if(firstdialog.isShowing())
firstdialog.dismiss();
}
});
I would like to display an ok cancel dialog to the user and I would like to know if use pressed ok, cancel, or if he chose to just dismiss the dialog by clicking elsewhere on the screen or pressing back button.
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
final EditText input = new EditText(MainActivity.this);
builder.setView(input);
builder.setPositiveButton("OK", new DialogInterface.OnClickListener()
{
#Override
public void onClick(DialogInterface dialog, int which)
{
// ok stuff
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener()
{
#Override
public void onClick(DialogInterface dialog, int which)
{
// cancel stuff
}
});
builder.setOnDismissListener(new DialogInterface.OnDismissListener()
{
#Override
public void onDismiss(DialogInterface dialog)
{
//dismiss stuff
}
});
builder.show();
The problem here is that whenever user presses ok button, dismiss listener gets triggered right after.Is there any way to not trigger dismiss listener if user presses button?
I do realize that I can use a boolean flag, but I am hoping that there is actually an elegant solution.
I am not searching for solution on how to prevent dialog from being dismissed. I am searching for solution on how to prevent dismiss listener from being triggered when ok button is pressed and dialog gets dismissed.
I think what you need is setOnCancelListener().
setOnDismissListener() will be called for any reason. It means if dialog will disappear from the screen either due to Ok/Cancel button press or screen touch or back button or home button press, setOnDismissListener() will be called.
Sets the callback that will be called when the dialog is dismissed for
any reason.
If you are interested in listening for all cases where the dialog is
dismissed and not just when it is canceled, see setOnDismissListener
So workaround it what you have mentioned, check using some boolean flags and handle it.
You can again show that dialog after being dismissed!
You can try CustomViewDialog by using
LayoutInflater myDialog = getLayoutInflater();
View convertView = (View) myDialog.inflate(R.layout.MyLayoutXmlFile, null);
Also don't use positive or negative buttons only use buttons in the dialog layout.
In one of my activity, there is a button, which when clicked opens a PopupWindow.
My intention is, when the PopupWindow is opened and user clicks anywhere on screen except the popup area, the PopupWindow should be dismissed.
For this I have set:
// onClick()
myPopupWindow.setOutsideTouchable(true);
myPopupWindow.setFocusable(false);
Issue is it works fine & PopupWindow gets dismissed when I click anywhere outside, but if I click on the button that generated this PopupWindow, then that event is consumed and PopupWindow first gets closed and then gets opened again.
I tried moving my button onclick() code to onTouch().
But if I return true, then button consumes every event & opens popup again and again, even with slightest drag while touching the screen.
If I return false, it behaves same as in onClick() & opens the popup again when button is touches back.
So how can I dismiss the PopupWindow even when clicked on the button?
Just disable that button after it has been clicked so that it can't be clicked when the pop-up window is displaying. button.setEnabled(false);
I solved a similar problem as follows:
settingsImageButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (!settingsMenu.isShowing()) {
settingsMenu.showAsDropDown(settingsImageButton);
settingsImageButton.setEnabled(false);
}
}
});
settingsMenu.setOnDismissListener(new PopupWindow.OnDismissListener() {
#Override
public void onDismiss() {
new AsyncTask<Void, Void, String>() {
#Override
protected String doInBackground(Void... params) {
return null;
}
#Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
settingsImageButton.setEnabled(true);
}
}.execute();
}
});
I call the method setEnabled() asynchronously, because onDismiss event is earlier than onClick event.
The below alert dialog requires me to click whichever button I click twice in order to close the dialog window and after an hours googling, I can't find the answer. I am sure it is staring me in the face but I just can't see it.
Edit: More searching has led me to believe the dialog is actually opened twice and it is occuring here:
asset_id_text_view.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
showAssetIDDialog();
return true;
}
});
Edit: started out with dialog.dismiss(); and some googling suggested trying dialog.cancel(); Neither of which were successful for me.
public void showAssetIDDialog() {
// TODO Auto-generated method stub
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
CreateTicketActivity.this);
// set title
alertDialogBuilder.setTitle("Enter Asset ID");
alertDialogBuilder.setCancelable(true).setMessage(
"How would you like to proceed?");
alertDialogBuilder.setPositiveButton("Enter text",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
//enterTextDialog();
dialog.cancel();
}
});
alertDialogBuilder.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
// create alert dialog
AlertDialog alertDialog = alertDialogBuilder.create();
// show it
alertDialog.show();
}
cancel() - Cancel the dialog. This is essentially the same as calling dismiss(), but it will also call your DialogInterface.OnCancelListener (if registered).
just cancel() will cancel the listener registerred on DialogInterface.
dismiss() - Dismiss this dialog, removing it from the screen. This method can be invoked safely from any thread. Note that you should not override this method to do cleanup when the dialog is dismissed, instead implement that in onStop().
Use dialog.dismiss() for your solution
http://developer.android.com/guide/topics/ui/dialogs.html#DismissingADialog
Please refer the above link for further query.
So it looks like the problem was indeed with the OnTouchListener
The code was doing what I had asked it to do and that was to open an AlertDialog every time there was a motionEvent. That makes at least 2 times for every touch
by including a switch statement, I was able to only trigger the opening of the alertDialog when the screen was pressed and not also when the screen was released as follows:
asset_id_text_view.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()){
case MotionEvent.ACTION_DOWN:
showAssetIDDialog();
break;
default:
break;
}
});
You can call dialog.dismiss() in the OnClickListener of the buttons to close the dialog.
Use dialog.dismiss();
Dismiss this dialog, removing it from the screen. This method can be
invoked safely from any thread.
alertDialogBuilder.setPositiveButton("Enter text",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
//enterTextDialog();
dialog.dismiss();
}
});
I have a dialog:
final Dialog dialog = new Dialog(context);
dialog.setContentView(R.layout.location_dialog);
dialog.setTitle("My dialog");
dialog.setMessage("My dialog's content");
dialog.setCancelable(true);
dialog.setCanceledOnTouchOutside(true);
dialog.show();
I want to be able to detect touches over and outside the dialog box's lines.
I can easily detect any touches outside the dialog box area with the build-in method
dialog.setCanceledOnTouchOutside(true);
But how can I detect the touches inside this area?
Create an extension of Dialog and override necessary method: dispatchTouchEvent or onTouchEvent (From docs: This is most useful to process touch events that happen outside of your window bounds, where there is no view to receive it.)
Updated:
#Override
public boolean dispatchTouchEvent(MotionEvent ev) {
Rect dialogBounds = new Rect();
getWindow().getDecorView().getHitRect(dialogBounds);
if (dialogBounds.contains((int) ev.getX(), (int) ev.getY())) {
Log.d("test", "inside");
} else {
Log.d("test", "outside");
}
return super.dispatchTouchEvent(ev);
}