Automatically close of Dialog box without any click in Android app - android

Can we create an Alert Dialog box which will appear after doing some activity and automatically get closed without any click (whether any button click or click outside the alert dialog box)?
Like, if we can add some delay to the dialog box to close after some specified seconds.
Any help would be appreciated.
Thanks

Better way to automatically close the alert dialog box would be to use Handler to delay the Dialog box.
Example:
new Handler().postDelayed(new Runnable() {
public void run() {
Dialog.dismiss();
}
}, 2000);
Here 2000 specifies number of milliseconds of delaying the Alert dialog box.

http://developer.android.com/guide/topics/ui/notifiers/toasts.html
This should do the job. A toast does not need a click, and can be timed.

Related

Hide dialog in kotlin on certain conditions

Is it possible to achieve this in Kotlin Android studio.
I have two alert dialog
Dialog one shows on one event to notify users that the event is loading.
Dialog 2 shows when the first event finish loading.
The current results, if dialog one shows, within 2 seconds the activity finish loading and then shows dialog 2.
How can I hide dialog one, on condition of when dialog 2 shows?
This is my alert dialog code
AlertDialog.Builder(requireActivity())
.setTitle("Status upload")
.setMessage("Your image was successfully uploaded!") // Specifying a listener allows you to take an action before dismissing the dialog.
// The dialog is automatically dismissed when a dialog button is clicked.
.setPositiveButton(android.R.string.yes) { dialog, which ->
// Continue with delete operation
} // A null listener allows the button to dismiss the dialog and take no further action.
.setIcon(R.drawable.ic_check_success)
.show()
you can do this where the event ends
dialog1.dismiss()

Dialog: confirmation when dismissed

Topic: I want to be able to cancel a dismiss call on a dialog.
I am entering information thorugh a dialog. When data inside the dialog has changed and the user dismisses the dialog without saving (by pressing back or clicking outside of the dialog), I want to be able to prevent that dismis by showing a confirmation dialog, that asks the user if he really wants do dismiss the dialog.
An analagy for what I am looking for is in VBA, where the cancel-variable of a beforeSave-listener can be set to "true", so that the file is not saved, even though save is pressed.
I could not find a solution that I can place inside the dismissListener of the dialog.
Thanks a lot in advance and best regards!
You can maintain the state of dialog open/ close in a boolean and handle it. When you open the dialog you make it true and when user clicks back or outside the dialog check that boolean and show the alert pop-up and when dialog closes (dismisses) make boolean to false.
user these methods on your dialog view to prevent cancel dialog
ProgressDialog progressDialog = new ProgressDialog(this);
progressDialog.setCanceledOnTouchOutside(false);
progressDialog.setCancelable(false);
progressDialog.show();

How to hold the activity when the dialog is open

I am working with Android, and I open a Dialog using myDialog.show(); from the Activity, this Dialog has a Button and when I click that button the Dialog closes without problem using this.hide();.
this is the part of code where I have the question :
myDialog.show();
Toast.makeText(this, "the dialog is closed", Toast.LENGTH_SHORT).show();
the Toast is displayed during myDialog is open, and I thought that when myDialog is open, this hold my Activity and the Toast can not be displayed, but it is not the case.
So what I want is that myDialog once opened hold the Activity and when it is closed the Activity continue to the next instruction which is the Toast
just Override onDismiss in your dialog and put your toast in there. onDismiss gets called when the dialog is closing.
of in your activity implement OnDismissListener and set the listener in your dialog
I think that a similar question is asked here, and the solution that proposed is to create your myDialog.setCancelable(false);
I find this here :
If you want to literally not have the function that brings up the dialog return until the dialog is closed, you're in for some trouble. That's not the way the Android UI works.....

Alert dialog is opening two times results crash of application

In my android app i am trying to click a button which does some calculation result a alert dialog. asking user yes or not.
Clicking yes perform the action and No dismiss the alert dialog.
Problem is when i am clicking the button very fast it opens two alert box some times , pressing yes in first alert dialog does its action but pressing yes on second result in crash.
What will be the best approach to solve this problem . Actually this is simple subjective logic that's why i am not adding code here .
Simply add:
if(dialog != null && !dialog.isShowing()) {
dialog.show();
}
To your onClick() method. This checks to see if the dialog is showing or not, and only shows it if it isn't already visible.
You'll have to replace dialog with whatever your instance is called.

How to create waiting window ( like dialog box )

In my application i need to send some information to server.
This action take time - and i want to show some 'waiting sign' in the meanwhile.
In the main Activity i dont have any place to add progess bar that will show the 'waiting' - so i want to pop up something like dialog box that will show the waiting process.
How to pop up / create this dialog box ( light box ) ?
You can display a Progress Dialog
Here's how you can do it -> http://android-er.blogspot.com/2011/07/display-indeterminate-progress-bar-on.html
mydialog = new Dialog(SplashScreen.this,
R.style.Theme_Transparent);
mydialog.addContentView(new ProgressBar(SplashScreen.this),
new LayoutParams(40, 40));
mydialog.show();
code to show dialog. and mydialog.dismiss() for dismiss dialog.

Categories

Resources