I have the following inside the method onCreate()
and it must necessarily be there:
do {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle(R.string.contacto).setIcon(R.drawable.rudeboys_icon);
builder.setMessage("hola");
builder.show(); } while(timer_dialog<10);
Which shows a dialog with the text "hello" and my timer_dialog that will be my time of 10 seconds, but in the onCreate I don't know how to put it.
In a function I know it is something like :
timer = new CountDownTimer(tiempo_dialog * 1000, 1000) {
@Override
public void onTick(long millisUntilFinished) {
crono.setText("" + String.format(FORMAT, TimeUnit.MILLISECONDS.toHours(millisUntilFinished),
TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished) - TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(millisUntilFinished)),
TimeUnit.MILLISECONDS.toSeconds(millisUntilFinished) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished))));
}
@Override
public void onFinish() { //finalizar dialog } }
How can I do it ? I have put a do-while , is it the most efficient?
The Builder is only for building the dialog. Check if it is shown or discard it you have to do in the dialog
In your code you create 10 dialogs in a
while
. Better close the dialogue with anHandler
after time.The Handler:
You add a Handler so that after 10 seconds it closes.
You can use a handler: How to update UI or method after a few seconds?
For example, if you want to call some method after 10 seconds:
First you create the dialog and show it, when the dialog is created you can create a handler that calls the method
dismiss()
to close it. If you are in a Fragment, the context to use would be obtained bygetActivity()
:Example:
As for your code, you are creating and displaying several
AlertDialog
while the value of the variabletimer_dialog
is less than 10...this is wrong , you just need to create an instance of the
AlertDialog
.To fix the problem that the screen closes when you click outside, you just have to create the dialog like this:
Greetings.
in my case using a DialogFragment, declare the Dialog as final
before(without adding the timer):
later:
then add a Handler to create the timer