4

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

StackExchange User
  • 1,222
  • 14
  • 35
Arun
  • 59
  • 1
  • 1
  • 7

3 Answers3

10

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();
          }
        });
ρяσѕρєя K
  • 132,198
  • 53
  • 198
  • 213
  • 1
    Thanks, I got where it was wrong. changed to View popupView = inflater.inflate(R.layout.about_popup, null, false); – Arun Mar 02 '13 at 06:07
1
CustomDialogClass cdd = new CustomDialogClass(this,
                "Internet Connection Error",
                "Sorry, no internet connection.Feeds cannot be refreshed",
                "Alert");
        cdd.show();

public class CustomDialogClass extends Dialog implements
    android.view.View.OnClickListener {
String txtTitle, txtText, txtType;
Button btn;
TextView alertText;
ImageView imgVw;

public CustomDialogClass(Context context, String title, String text,
        String type) {
    super(context);
    txtTitle = title;
    txtText = text;

}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.alert_layout);
    imgVw = (ImageView) findViewById(R.id.iconImgview);

    alertText = (TextView) findViewById(R.id.AlertText);
    alertText.setText(txtText);

    btn = (Button) findViewById(R.id.dismis_dialog);
    btn.setOnClickListener(this);
}

@Override
public void onClick(View v) {
    dismiss();
}
Sanket Pandya
  • 1,095
  • 7
  • 21
0

Change This

  View popupView=inflater.inflate(R.layout.about_popup, null, false);
    Button close = (Button) popupView.findViewById(R.id.okbutton);

to

 Button close = (Button) pw.findViewById(R.id.okbutton);
Sunny
  • 14,522
  • 15
  • 84
  • 129