Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Closing the Popup in Android [duplicate]

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

like image 918
Arun Avatar asked Dec 21 '22 09:12

Arun


1 Answers

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();
          }
        });
like image 196
ρяσѕρєя K Avatar answered Jan 08 '23 16:01

ρяσѕρєя K