Cerrar un dialogo en Android pasado un cierto tiempo.
En el siguiente ejemplo vamos a mostrar como se cerraría un dialogo en Android pasado 1 segundo desde que fue abierto. Para ello vamos a usar el ejemplo del post anterior, donde aprendimos a crear un diálogo con un SeekBar.
Por lo que no vamos a copiar el código de nuevo (ver post anterior)

public void abrir_dialogo(){
     LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    final View layout = inflater.inflate(R.layout.barra_ajuste, (ViewGroup) findViewById(R.id.my_dialog_root_element));
    

    AlertDialog.Builder builder = new AlertDialog.Builder(ACapturar.this).setView(layout);
    final AlertDialog alertDialog = builder.create();
    alertDialog.show();
    ....

    Timer myTimer;
     myTimer = null;
     myTimer = new Timer();
     myTimer.schedule(new TimerTask() {
           @Override
            public void run() {
                alertDialog.cancel(); // call the cancel method directly
            }
        }, 800); //tiempo en milisegundos
     }

  }

No hay comentarios: