Close Dialog after 10 seconds open

Asked

Viewed 866 times

3

I would like the dialog close after 10 seconds. I can only do it by clicking the button.

Follows the code.

AlertDialog.Builder alert = new AlertDialog.Builder(this);
        WebView wv = new WebView (this);
        WebSettings webSettings = wv.getSettings();
        webSettings.setJavaScriptEnabled(true);
        wv.loadUrl("http://192.168.200.233:8888/Propagandas/Propagandas.html");
        wv.setWebViewClient(new WebViewClient() {

            @Override
            public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
                view.loadUrl(request.getUrl().toString());
                return true;
            }
        });

        alert.setView(wv);
        alert.setNegativeButton("Fechar", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int id) {
                    dialog.dismiss();
                }
            });
            alert.show();
        Handler handler = new Handler();
        handler.postDelayed( this, 2000 );

It is possible?

1 answer

6


10 seconds in milliseconds would be 10000. See below for an example how it would be using the method setOnDismissListener() of its dialogue box:

Observing: Create a variable of type Dialog to receive your alert.show(), so that you can use the method dimiss().

... 
//alert.show();
final Dialog dialog = alert.show();

final Handler handler = new Handler();
final Runnable runnable = new Runnable() {
    @Override
    public void run() {
        // verificar se a caixa de diálogo está visível
        if (dialog.isShowing()) {
            // fecha a caixa de diálogo
            dialog.dismiss();
        }
    }
};

alert.setOnDismissListener(new DialogInterface.OnDismissListener() {
    @Override
    public void onDismiss(DialogInterface dialog) {
        handler.removeCallbacks(runnable);
    }
});

handler.postDelayed(runnable, 10000);

There you need to declare your AlertDialog.Builder globally, outside the onCreate. Behold:

public AlertDialog.Builder alert;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    alert = new AlertDialog.Builder(this);
    // restante do conteúdo
  • When I put in the code, you ask me to put Alert at the end and when I put in the code you tell me which method can’t be applied, you know what I need to do?

  • @Eduardosantos declares Alert as Global, outside onCreate. Only the variable.

  • Cannot find Symbol method isshowing, what appears.

  • You know what it can be?

  • @Eduardosantos made a change using Dialog. Look now, it worked pretty here with me. = D

  • 1

    Thank you very much

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.