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();
}
});