0

I would like to use the popup window for my UI activity. Actually, I have a button in my main activity page which on clicked should open like a dialog window.

In that dialogue window, I want to have two other buttons in it which on clicked should pass some value for every click**(without the window being disappeared)** and if there is no click for defined time (time-out) it should vanish and return to the main activity.

I can do this using "Intent" but i want to implement it with this automatically vanishing dialog window after time-out. How can i do this.? please help me out with this problem.

Thanks in advance,

frogatto
  • 28,539
  • 11
  • 83
  • 129

1 Answers1

0

First declare one global varialbe for Handler to update the UI control from Thread, like below-

Handler mHandler = new Handler();

Now create one Thread and use while loop to periodically perform the task using the sleep method of the thread. Suppose you have menu_bt the object of your view from where you show/open the popup window than use thread to dismiss automatic the popup window when there is no click for defined time i.e. 10 seconds.

Here mpopup is the object of PopupWindow.

Use the below code for vanishing dialog window after time-out (i used 10 seconds).

Hope it will help you.

menu_bt.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View arg0) {

                View popUpView = getLayoutInflater().inflate(
                        R.layout.menu_popup, null); // inflating popup layout
                mpopup = new PopupWindow(popUpView, 400,
                        LayoutParams.WRAP_CONTENT, true); // Creation of popup
                mpopup.setAnimationStyle(android.R.style.Animation_Dialog);
                mpopup.showAsDropDown(menu_bt, 50, -10);
                Button btnOk = (Button) popUpView.findViewById(R.id.home);

                btnOk.setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mpopup.dismiss(); // dismissing the popup with button click
                    }
                });
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        // TODO Auto-generated method stub
                        while (true) {
                            try {
                                Thread.sleep(10000);
                                mHandler.post(new Runnable() {

                                    @Override
                                    public void run() {
                                        // TODO Auto-generated method stub
                                        // automatic dismissing the popup after 10 seconds
                                        mpopup.dismiss();
                                    }
                                });
                            } catch (Exception e) {
                                // TODO: handle exception
                            }
                        }
                    }
                }).start();
            }
        });
Priyanka
  • 677
  • 5
  • 20