I have the following function which calls a popup from a menu button click. It has an ok button to close the popup. But the onclick
function is not initiated on a button press. Also, I need to close the popup when the back button is pressed.
LayoutInflater inflater = (LayoutInflater) MainActivity.this
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final PopupWindow pw = new PopupWindow(inflater.inflate(R.layout.about_popup, null, false),400,440, true);
pw.showAtLocation(lv, Gravity.CENTER, 0, 0);
View popupView=inflater.inflate(R.layout.about_popup, null, false);
Button close = (Button) popupView.findViewById(R.id.okbutton);
close.setOnClickListener(new OnClickListener() {
public void onClick(View popupView) {
pw.dismiss();
}
});
Thanks
currently you are passing different instance of View to PopupWindow
and try to find Button in difference instance use same instance which u have passed in PopupWindow to find button . Change your code as :
LayoutInflater inflater = (LayoutInflater) MainActivity.this
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View popupView = layoutInflater.inflate(R.layout.about_popup, null, false);
final PopupWindow pw = new PopupWindow(popupView,400,440, true);
pw.showAtLocation(lv, Gravity.CENTER, 0, 0);
Button close = (Button) popupView.findViewById(R.id.okbutton);
close.setOnClickListener(new OnClickListener() {
public void onClick(View popupView) {
pw.dismiss();
}
});
second way is use PopupWindow
instance to find Button on Current Window inside of inflating layout again for button as :
Button close = (Button) pw.findViewById(R.id.okbutton);
close.setOnClickListener(new OnClickListener() {
public void onClick(View popupView) {
pw.dismiss();
}
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With