I have a popup button that is working fine. When i click on it it gets dismissed.
But i also want to add in a code for dismissing the popup after 5 secs if the user does not take any action on it? Is that possible?
The current code
final ImageButton rredButton=(ImageButton)findViewById(R.id.RredButton);
rredButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
LayoutInflater layoutInflater
= (LayoutInflater)getBaseContext()
.getSystemService(LAYOUT_INFLATER_SERVICE);
View popupView = layoutInflater.inflate(R.layout.popupright, null);
final PopupWindow popupWindow = new PopupWindow(
popupView,
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
Button btnNxtScr = (Button)popupView.findViewById(R.id.nextscreen);
btnNxtScr.setOnClickListener(new Button.OnClickListener(){
@Override
public void onClick(View v) {
Intent myintent1 = new Intent(colorActivity.this,colorBlueActivity.class);
startActivity(myintent1);
}
});
popupWindow.showAtLocation(rredButton, Gravity.CENTER, 0, 0);
//---
popupWindow.setFocusable(true);
popupWindow.update();
//---
}});
This is my updated code. What is wrong?
final ImageButton rredButton=(ImageButton)findViewById(R.id.RredButton);
rredButton.setOnClickListener(new View.OnClickListener() {
private CountDownTimer mPopUpDismissTimer;
public void onClick(View arg0) {
LayoutInflater layoutInflater
= (LayoutInflater)getBaseContext()
.getSystemService(LAYOUT_INFLATER_SERVICE);
View popupView = layoutInflater.inflate(R.layout.popupright, null);
final PopupWindow popupWindow = new PopupWindow(
popupView,
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
getPopUpDismissTimer(3000, 1000);
mPopUpDismissTimer.start();
}
private void getPopUpDismissTimer(long millisInFuture, long countDownInterval) {
mPopUpDismissTimer = new CountDownTimer(millisInFuture, countDownInterval) {
@Override
public void onFinish() {
Button btnNxtScr = (Button)popupView.findViewById(R.id.nextscreen);
btnNxtScr.setOnClickListener(new Button.OnClickListener(){
@Override
public void onClick(View v) {
Intent myintent1 = new Intent(colorActivity.this,colorBlueActivity.class);
myintent1.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(myintent1);
};
});
popupWindow.showAtLocation(rredButton, Gravity.CENTER, 0, 0);
//---
popupWindow.setFocusable(true);
popupWindow.update();
//---
}
@Override
public void onTick(long millisUntilFinished) {
}
};
}});
Get a CountDownTimer like this,
private CountDownTimer mPopUpDismissTimer; // instance variable, put in your activity class
}
Now, invoke this Count Down Timer where you want to dismiss popup, like –