Question

i am new to android and trying to autoclose my inflated popup window after some seconds with some time counter . I have not idea ho to do with count timer ( 5 seconds ).

  LayoutInflater inflater = (LayoutInflater)screen.getSystemService(screen.LAYOUT_INFLATER_SERVICE);
                    layout =  inflater.inflate(R.layout.log_viewer,null);

                    AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
                    builder.setCancelable(true);
                    builder.setView(layout);

                    AlertDialog alertDialog = builder.create(); 
                    alertDialog.show();

                    Button btn0= (Button)layout.findViewById(R.id.btn0);
                    Button btn1= (Button)layout.findViewById(R.id.btn1);
                    Button btn2= (Button)layout.findViewById(R.id.btn2);
                    btn0.setOnClickListener(new OnClickListener() {

                        @Override
                        public void onClick(View v) {

                            Intent i =new Intent(Main_Activity.this,Act.class);
                            startActivity(i);
                            overridePendingTransition(R.anim.animation,R.anim.animation2);
                        }
                    });  
                    btn1.setOnClickListener(new OnClickListener() {

                        @Override
                        public void onClick(View v) {

                            Intent i =new Intent(Main_Activity.this,Activity2.class);
                            startActivity(i);
                            overridePendingTransition(R.anim.animation,R.anim.animation2);
                        }
                    });  
                    btn2.setOnClickListener(new OnClickListener() {

                        @Override
                        public void onClick(View v) {

                            Intent i =new Intent(Main_Activity.this,Activity1.class);
                            startActivity(i);
                            overridePendingTransition(R.anim.animation,R.anim.animation2);
                        }
                    });  


                    WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
                    Window window = alertDialog.getWindow();
                    lp.copyFrom(window.getAttributes());

                    lp.width = WindowManager.LayoutParams.MATCH_PARENT;
                    lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
                    window.setAttributes(lp);
Was it helpful?

Solution

You can close it like -

new Handler().postDelayed(new Runnable(){
    public void run() {
        popup.dismiss();                    
            }                   
}, 5 *1000);

OTHER TIPS

Kanak Sony' method is good, but it may cause some problem. If you leave the activity when your popupwindow is shown, an exception may caused. u should add a dismiss in onDestroy() method in your activity like this.

public void destroy(){
    if (popup != null) {
        popup.dismiss();
        popup = null;
    }
}

and in handler:

new Handler().postDelayed(new Runnable(){
public void run() {
    if (popup != null) {
        popup.dismiss();
        popup = null;
    }                    
        }}, 5 *1000);

hope it help u.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top