Android :Alert Dialog, Cancel Button - android

I have a dialog with five CheckedTextView. When I open dialog and Click on some CheckBoxes and than I click Cancel Button. But when I open dialog again. The changes made is saved. Why? How to cancel what I clicked when I press Cancel button ?

Android creates the dialog box just once, and then reuses it instead of recreating it. Thus, your onCreateDialog method is called only once. The second time, you get the same dialog, with the same check boxes in the same state.
To ensure that the dialog contains the correct data, override onPrepareDialog. This gets called every time right before the dialog is shown. From here, you can set the check boxes to the desired state.

Related

Android: View Visibility set to View.VISIBLE but View is not Visible

In my app (general construction described below), I have a timer, which the user can start by clicking an in-app button.
When the timer starts, a notification is shown. In the notification, there's a "STOP" button, which when clicked is supposed to open the app (if closed or killed) and then show a custom dialog.
The custom dialog is built of a bunch of views and a blur-background view.
There are two ways to open that dialog:
Clicking an in-app button triggers the method that opens this dialog.
Clicking the "STOP" button in the notification also triggers the same method (By calling a BroadcastReceiver that lives in the same fragment as the dialog, and in the BroadcastReceiver I call the method that opens the dialog - it all happens after the app opens (if it was closed or killed)).
The first method works - clicking the button opens the dialog when it's supposed to.
The second, however, opens the app but not the dialog (To clarify - opening the dialog means changing its views visibility to View.VISIBLE).
To check what goes wrong, I used a Toast message that shows the visibility of the dialog every time the method that opens the dialog gets called. The Toast message shows "VISIBLE", so that means the Views visibility is set to View.VISIBLE indeed - but the dialog is not shown, nor the blur-background.
General Construction: The app has multiple Fragments, stored inside a view pager, inside the MainActivity and the dialog lives in the main fragment.
Might Be Relevant: when clicking the notifications button, the app opens, but the notification panel stays fully opened. The Toast message shows behind the notification panel.
Also might be worth mentioning, that when I click the notification button while I didn't yet leave the app, the dialog pops up. Only after I leave the app and then come back (manually or by clicking the notification button) the dialog doesn't pop up.
dialog's XML: (stored inside of fragment_main.xml)
<com.google.android.material.circularreveal.CircularRevealFrameLayout
android:id="#+id/dialog_container"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="invisible"
app:layout_behavior="com.google.android.material.transformation.FabTransformationSheetBehavior">
<com.google.android.material.card.MaterialCardView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:cardPreventCornerOverlap="true">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="15.0dip">
//Here is the content of the dialog, textviews and custom buttons
</RelativeLayout>
</com.google.android.material.card.MaterialCardView>
</com.google.android.material.circularreveal.CircularRevealFrameLayout>
dialog opening method:
private void openDialog() {
//while "dialog" refers to the "dialog_container" in the dialog xml
dialog.setVisibility(View.VISIBLE);
fadeBlurIn(); //fades the blur-background view in
dialog.setClickable(true);
}
calling the method in the BroadcastReceiver: (stored inside MainFragment.java)
private final BroadcastReceiver endTimerReceiver = new BroadcastReceiver() {
#RequiresApi(api = Build.VERSION_CODES.M)
#Override
public void onReceive(Context context, Intent intent) {
.
.
.
boolean openDialog = {gets a boolean extra, works fine};
if (openDialog){
openDialog();
}
}
}
};
this BroadcastReceiver is attached to this fragment and works fine.
Why doesn't the dialog show?
And how can I fix it?
Problem Solved
What was the issue all about?
The problem was when re-opening the app after closing or leaving it, the main-fragment was re-created, and with it a new blur-background and a new custom dialog.
The BroadcastReceiver was stored in the old main-fragment, and so it referred to the old views (the old blur and dialog views)
The old views' visibility has changed, but the new ones didn't, so the visibility state change wasn't visible to the user.
But that's not all. Later on, I discovered that in addition to all that, even if I refer to the right view and try to display it, it's not displaying, and I found another problem - I try to change its visibility state too early.
Solution
Solving it was quite simple.
All I had to do was make the blur-background and the custom dialog static, so they'll not re-create every time a main-fragment is created. Then Their visibility state changes were visible to the user.
And the solution for the second half of the problem was even easier - all I had to do is put the command calling the method to show the dialog inside a Runnable, running in delay by a Handler. 10 ms of delay has done the job.
Although this thread's issue is solved by now, this issue still isn't.
Note to self
A good habit is to think ahead, knowing what objects I should set static, so that stuff like this won't happen frequently
By notification panel, do you mean it's a bar on top of the app activity?
If so, my guess is that opening the notification panel pushes the dialog outside the device screen. Perhaps the dialog is constrained to the notification panel?
I'm not very sure about this, so sorry if this turns out to be incorrect!
Also, posting some code might help.
You have made the view invisible and shown when clicked on either button or notification bar.
You can do is
Make the view visible and add the condition if called by Notification bar it remains as it is, if not then hide it and make it visible again on call on button.
Give the fix height and width to dialog
box and set the visibilty to "Gone".
android:visibility="gone"
After that call open dialog function and setVisibility to Visible.

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);

Cancel Dialogfragment click outside

I have a dialogfragment, which should dismiss, if i click outside of the dialog. I have no button or something else to close the dialog.
But before the dismiss is calling, I want to check a field in my dialog. If the check is positive, then the dialog should close, else not.
Which method can I override to do that? Or can I do it manual with a onTouchListener, which dismiss the dialog, if I'm clicking beside?
I would do following. I would add transparent button on background of your dialog fragment in order to fragment take all place and set onClickListener for that button. Maybe it's wierd but it's work ))

How to Display two alert dialogs in specific order?

I have two alert dialogs. Whenever app opens, the first dialog is shown. After some time, the second dialog is shown. But here problem is recently created dialog(second one) is in front. But I want the fist created dialog is in front and second one is behind the first one.
The problem with the dialog box is that you can not display two dialog box at a same time. You must hide first dialog box in order to display second one.
You can only display one dialog at the time, but you can "work around" this. When you are about to display your second dialog, you hide your first dialog, but your application remembers that the first dialog was open, so when you hide your second dialog you displays the first dialog again. In reality this will go so fast so the user is fooled that the dialog is layered over eachother(if they have the same dimensions).
But you cannot display a dialog on top of another dialog.
You can display first dialog in front :
When you are calling to display second one at that time call first dialog again so, that is in from view of your screen.
Let me know you understand this or not.. otherwise i will provide code for it..
You cant make two dialog active at same time either one should active and the other should inactive.In your case if you want the second should come background of fist then you have to call first dialog from the second.

Keep AlertDialog's DialogInterface opened

I have a custom AlertDialog where a user has to set password. There are two edittext views.
I compare them first if they match and if they are more than 3 characters long. If they don't match, I display a toast to alert the user. But after submitting and checking the dialog closes. How can I keep it opened until user inserts the correct values?
I was looking at doing something similar and I could not find a way do this with the standard AlertDialog as it is.
One possible way I found was to not specify any button listeners in your AlertDialog and instead place a view with your own custom buttons that perform the checks and then dismiss the dialog if necessary. I've not yet tried this to see how it works.
Another option is to create your own Custom dialog by subclassing Dialog.
You have to set a global variable like
boolean showAlert = true;
And attach a onClick listener to the AlertDialog and after cheking to see if there is need to show the alert again. If there is a need, you should show it again. You can`t keep it open if the user clicks a button from the AlertDialog. You have to recreate it again.

Categories

Resources