I am in a situation where i need an alert dialog to pop up after some seconds. I tried Handler with postDelayed(), it didn't work.
new Handler().postDelayed(new
Runnable() {
@Override
public void run() {
AlertDialog.Builder builder = new AlertDialog.Builder(
MyActivity.this);
builder.setTitle("My title... ");
builder. setMessage("my msg..");
builder. setPositiveButton("OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int which) {
Log. e("info", "OK");
}
});
builder.show();
}
});
}
}, 33000);
You have to add this piece of code below builder.show(); It works for me.
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
public void run() {
dialog.dismiss();
}
}, 1500); // 1500 seconds
Edit:
I use this in a different way that I could see in your case. Here you can see my complete code. In my case I only want that apears a "simulate" Progress Dialog, and after some time it disappears.
protected void ActiveProgressDialog() {
final ProgressDialog dialog = ProgressDialog.show(getActivity(), "", getResources().getString(R.string.carregantInfo), true);
dialog.show();
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
public void run() {
dialog.dismiss();
}
}, 1500); // 1500 milliseconds
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With