AlertDialog buttons always close the dialog once clicked - android

I'm using an AlertDialog with 2 buttons (one positive, one negative).
I've set a handler for those buttons which doesn't mention dismiss. Indeed after clicking, I don't want the dialog to close. However, it seems that as soon as we click on a positive/negative button on an AlertDialog, it always get closed.
Is there a way to prevent the dialog from closing after clicking on any button ?
Thanks,
Vincent

Use custom layout for Dialog and you will be happy!

Use a custom dialog, not an AlertDialog.

You can also create a new Activity and assign it the theme dialog in your manifest with
<activity android:name="MyDialogActivity" android:theme="#android:style/Theme.Dialog" />

Related

How to bypass android dialog outside click

What I want to do is to show the dialog message but make it still possible to click on the items behind. On any click, the dialog would dismiss
Right now I need to click once to dismiss the dialog and a second time to click on a field.
It is something possible ? Or is there an alternative to using Dialogs?
Edit: Solution found by adding Layout Flags to the window.
In kotlin:
dialog.window?.setLayout(ConstraintLayout.LayoutParams.FLAG_NOT_FOCUSABLE, ConstraintLayout.LayoutParams.FLAG_NOT_FOCUSABLE)
By using Dialog you cannot achieve what you are looking for. instead of using Dialog USE FRAGMENT.
The examples are given in official documentation here.
You Can define a full layout in the background of the dialog and set onClick listener to that layout

Android Dialog out of dialog click

I made a custom dialog (extends Dialog) and then make the object in Activity Sample.class
Then if I click one button in the Activity and the custom dialog shows up.
Here, the problem is, since the custom dialog contains the EditText, the soft keyboard is needed and it shows up, but if I click(tab) the screen outside of the dialog, the "cancel" listener is called so that the dialog is disappeared. However, what I want to implement is when I click the screen outside of the dialog, only just keyboard disappearing. Can anyone help me?
Use setCanceledOnTouchOutside(false) on the dialog instance. This will stop dismissing the dialog. But I am not sure the soft keypad will go away with this.
use this,
dialog.setCanceledOnTouchOutside(false);

When does touching the screen outside an AlertDialog cancel the AlertDialog?

Suppose you have an AlertDialog with two buttons A and B. I have found that on some devices and some versions of android, if you touch any area of the screen around the dialog, the AlertDialog disappears. On other devices you are forced to select either A or B, so there is no way I can allow the user to cancel the action without adding a third ('Cancel') option to the AlertDialog. Is there any way to determine programmatically whether the third option is required?
You can control it with dialog.setCanceledOnTouchOutside(true);
For more information :
How to dismiss the dialog with click on outside of the dialog?
Dialog dialog = new Dialog(context)
dialog.setCanceledOnTouchOutside(true);

Display dialog box before intent activity starts in android

I'm creating an application in which I've to display a dialog box. This dialog box having an edittext and a button. I want to display this dialog box before activity starts. please help me.
Create an instance of dialog in onCreate(); and show it in onResume();
you can use a dialog fragment:
http://developer.android.com/reference/android/app/DialogFragment.html
this is an example of how to use it:
check out this tutorial
there are some ways where what you can do is you can have a activity which is registered as
<activity android:theme="#android:style/Theme.Dialog" />
at manifest and show the activity before yours activity that it will be an appearance of dialog beside this
visit
How do I display a dialog in android without an Activity context?

Dialog with outside touchable

In an android application, I want to display a Dialog on screen and in the same time to allow user to click on the application's UI when the the Dialog is open
can this be done?
if no, what can I use instead of Dialog?
I don't think you can do this with Dialog. But you can do it with PopupWindow using PopupWindow.setOutsideTouchable(true);.
Showing up a dialog over your Activity, will make your Activity go into onPause() so you won't be able to actually handle things inside that activity anymore until the dialog is dismissed.
If you want to have some view, overlay your original activities view you'll be looking for into the direction of FrameViews, which can overlap other Views.

Categories

Resources