Question

I've an Android activity that opens up a pop_up window when pressing some button, this pop_up contains a cancel button that dismisses it when pressing, but when the pop_up opens and I press the cancel button this button doesn't work and i don't know why, this is my code :

Java:

private PopupWindow pw;
private ImageButton TVShowposter;

    private void initiatePopupWindow(String section) {
        try {
            View layout = null;
            // to get the instance of the LayoutInflater
            LayoutInflater inflater = (LayoutInflater) Media.this
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            // Inflate the view from a predefined XML layout
               if (section.equals("Music")) {

                layout = inflater.inflate(R.layout.music_popup,
                        (ViewGroup) findViewById(R.id.music_pop));

                // create a 300px width and 470px height PopupWindow
                pw = new PopupWindow(layout,
                        ViewGroup.LayoutParams.WRAP_CONTENT,
                        ViewGroup.LayoutParams.WRAP_CONTENT, true);
                // display the popup in the center
                pw.showAtLocation(layout, Gravity.CENTER, 0, 0);

                Button cancel = (Button) findViewById(R.id.back_music);
                cancel.setOnClickListener(onCancelClick);

                }

             } catch (Exception e) {
                    e.printStackTrace();
             }
          }

    private OnClickListener onCancelClick = new OnClickListener() {
        public void onClick(View v) {

            pw.dismiss();

        }
    };

XML(button part):

      <LinearLayout
           android:orientation="vertical"
           android:layout_width="fill_parent"
           android:layout_height="wrap_content"
           android:gravity="center" >
        <Button 
          android:id="@+id/back_music"
          android:background="@drawable/gradient_popups"
          android:text="@string/cancel"
          android:layout_gravity="left"
          android:layout_width="wrap_content"
          android:layout_height="fill_parent"
          android:paddingLeft="5dp"
          android:paddingRight="5dp"
          android:paddingTop="2dp"
          android:paddingBottom="2dp"
          android:textColor="@android:color/white"
          android:clickable="true" />
        <TextView
           android:id="@+id/music_text" 
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:text="@string/music"
           android:textColor="@android:color/white"
           android:textSize="13sp"
           android:layout_gravity="center" />
        </LinearLayout>
Was it helpful?

Solution 2

your button actually have reference problem you just need to call findviewbyid method from the layout which you have inflated for popup, like this:--

Button cancel = (Button) layout.findViewById(R.id.back_music);
            cancel.setOnClickListener(onCancelClick);

OTHER TIPS

You just create button like below:

Button close=(Button)layout.findViewById(R.id.close_btn);

This is for refernce.

close.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                popupWindow.dismiss();
            }
        });

Your error in your code at this place:

  Button cancel = (Button) findViewById(R.id.back_music);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top