Search code examples
androidpopupwindowcountdowntimerandroid-popupwindow

CountDownTimer stops when PopupWindow appears


In an Android activity I'm using a CountDownTimer as follows:

    timer = new CountDownTimer(time, 1000) {
        public void onTick(long millisUntilFinished) {
            timeView.setText(String.format(Locale.getDefault(), "%ds", millisUntilFinished / 1000));

            if (millisUntilFinished <= 10000) {
                // Blinking text
                ViewUtil.setBlinking(timeView,Animation.INFINITE);
            } else {
                timeView.clearAnimation();
            }
        }

        public void onFinish() {
            timeView.clearAnimation();
            timeView.setText("0s");
    }.start();

This timer just counts backwards in seconds. In the same activity I also showing a popup window using PopupWindow class as follows:

    LayoutInflater inflater = (LayoutInflater) getApplicationContext().getSystemService(LAYOUT_INFLATER_SERVICE);
        ConstraintLayout mRelativeLayout = (ConstraintLayout) findViewById(R.id.task_layout);
        View customView = inflater.inflate(R.layout.popup_window,mRelativeLayout,false);
        mPopupWindow = new PopupWindow(
                customView,
                LayoutParams.WRAP_CONTENT,
                LayoutParams.WRAP_CONTENT
        );
        mPopupWindow.setElevation(5.0f);
        mPopupWindow.showAtLocation(mRelativeLayout, Gravity.CENTER,0,0);

The problem is that as soon as the popup window appears, the CountDownTimer stops somehow. What I want is that the CountDownTimer runs also when the popup window appears and the background view gets still updated.

How can this be done?


Solution

  • You are running CountDownTimer in UI thread, when you popup window the thread goes for that window, however I searched through PopupWindow documentation but it has weak documentation couldn't found much

    Using CountDownTimer in a background solve it. Using Asynctask, Service or Runnable