SHARE
TWEET

Untitled

a guest Oct 12th, 2017 46 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import java.util.Timer;
  2. import java.util.TimerTask;
  3.  
  4. import android.app.Activity;
  5. import android.app.AlertDialog;
  6. import android.content.DialogInterface;
  7. import android.os.Bundle;
  8. import android.os.Handler;
  9. import android.os.Message;
  10. import android.view.View;
  11. import android.widget.Button;
  12.  
  13. public class AlertDialogStudy extends Activity {
  14.     /** Called when the activity is first created. */
  15.     @Override
  16.     public void onCreate(Bundle savedInstanceState) {
  17.         super.onCreate(savedInstanceState);
  18.         setContentView(R.layout.main);
  19.  
  20.         // get button
  21.         Button btnShow = (Button)findViewById(R.id.btn_show);
  22.         btnShow.setOnClickListener(new View.OnClickListener() {
  23.  
  24.             @Override
  25.             public void onClick(View v) {
  26.                 AlertDialog.Builder builder = new AlertDialog.Builder(v.getContext());
  27.                 builder.setTitle("Auto-closing Dialog");
  28.                 builder.setMessage("After 2 second, this dialog will be closed automatically!");
  29.                 builder.setCancelable(true);
  30.  
  31.                 final AlertDialog dlg = builder.create();
  32.  
  33.                 dlg.show();
  34.  
  35.                 final Timer t = new Timer();
  36.                 t.schedule(new TimerTask() {
  37.                     public void run() {
  38.                         dlg.dismiss(); // when the task active then close the dialog
  39.                         t.cancel(); // also just top the timer thread, otherwise, you may receive a crash report
  40.                     }
  41.                 }, 2000); // after 2 second (or 2000 miliseconds), the task will be active.
  42.  
  43.             }
  44.         });
  45.     }
  46. }
RAW Paste Data
Top