Question

I am 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

Did you consider using Toast with a custom layout? See Toast documentation on Android developers site for an example.

Or else you can use an Handler

Handler handler = new Handler();
handler.postDelayed(new Runnable() {

    @Override
    public void run() {
        // close your dialog
        alertDialog.dismiss();
    }

}, 10000); // 10,000 ms delay

OTHER TIPS

extend your own PopupWindow window class like this

public class ToolTipWindow extends PopupWindow implements PopupWindow.OnDismissListener { 

    private long closeDelayTime;
    private Handler mHandler;
    private OnDismissListener mOnDismissListener;

    public ToolTipWindow(Context context) {
        super(context);
        super.setOnDismissListener(this);
    }

    public ToolTipWindow(Context context, long closeDelayTime) {
        this(context);
        this.closeDelayTime = closeDelayTime;
    }

    @Override
    public void setOnDismissListener(OnDismissListener onDismissListener) {
        mOnDismissListener = onDismissListener;
    }

    //override all show or update method of super class that you use tho call onshow
    @Override
    public void showAtLocation(View parent, int gravity, int x, int y) {
        onShow();
        super.showAtLocation(parent, gravity, x, y);
    }

    public void onShow() {
        schedule();
    }

    private void schedule() {
        if (closeDelayTime > 0) {
            if (mHandler == null) {
                mHandler = new Handler();
            }
            mHandler.removeCallbacks(closeRunnable);
            mHandler.postDelayed(closeRunnable, closeDelayTime);
        }
    }

    @Override
    public void onDismiss() {
        if (mHandler != null) {
            mHandler.removeCallbacks(closeRunnable);
        }
        if (mOnDismissListener != null) {
            mOnDismissListener.onDismiss();
        }
    }

private Runnable closeRunnable = new Runnable() {
    @Override
    public void run() {
        dismiss();
    }
};

}

Here I only override showAtLocation method, if you use another PopUpWindow methods to show that, override that method and call onShow() inside it to automatically schedule is called.

Somewhere in code I call mHandler.removeCallbacks(closeRunnable), it is so important to call this because if widow is closed before time passed, it avoid crash.

If you want to perform an action after a certain delay, you can do the following:

final long TOTAL_TIME = 5000; // miliseconds

new Handler().postDelayed(new Runnable() {
    public void run() {
        // do action here
    }
}, TOTAL_TIME);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top