Cherylin
Cherylin

Reputation: 29

Close popup problem on button click

I create info_popup.xml ( simple, I have textview and imagebutton on popup). I show in my main activity, but I don't know how to close that popup on click on btnExitInfo button. What to put in click listener to close pw ? I tried with GONE but it doesn't work, it is still there .

 LayoutInflater inflater = (LayoutInflater) currentActivity
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            PopupWindow pw = new PopupWindow(inflater.inflate(R.layout.info_popup,
                    null, false), 230, 230, true);

            pw.showAtLocation(currentActivity.findViewById(R.id.main),
                    Gravity.CENTER, 0, 0);
            final View popupView=inflater.inflate(R.layout.info_popup, null, false);

            ImageButton btnExitInfo=(ImageButton)popupView.findViewById(R.id.btnExitInfo);
            btnExitInfo.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    //popupView.setVisibility(View.GONE);


             }
         });

Upvotes: 1

Views: 4597

Answers (2)

Ravi
Ravi

Reputation: 11

The following code worked for me:

Declare pw at class level: PopupWindow pw;

                LayoutInflater inflater = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);

                final View vw = inflater.inflate(R.layout.activity_child, null, false);

                Button close = (Button) vw.findViewById(R.id.btnClose);
                close.setOnClickListener(new View.OnClickListener() {
                    public void onClick(View view) {
                        if(pw.isShowing()){
                            pw.dismiss();
                        }
                    }
                });

                Button next = (Button) vw.findViewById(R.id.btnProceed);
                next.setOnClickListener(new View.OnClickListener() {
                    public void onClick(View view) {
                        Intent myIntent = new Intent(view.getContext(), NextActivity.class);
                        startActivity(myIntent);
                    }
                });

                pw = new PopupWindow(vw, LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT, true);
                pw.setBackgroundDrawable(getResources().getDrawable(R.drawable.gradient_bg));
                pw.setOutsideTouchable(false);
                pw.showAtLocation(sv, Gravity.CENTER, 10, 10);

Upvotes: 0

stealthcopter
stealthcopter

Reputation: 14206

Try using dismiss:

 public void onClick(View v) {
  pw.dismiss();
 }

you'll need to add a final modifier to final pw so you can have it inside the onclick method:

final PopupWindow pw = new PopupWindow...........

Upvotes: 1

Related Questions