Tomislav Babic
Tomislav Babic

Reputation: 21

Android popup won't dismiss

I am trying to implement a popup in my application but it won't dismiss. The Popup is supposed to open when I click on a Plus button, and this works. However, it is supposed to close when I click on the Cancel button in popup layout but it doesn't. why?

Popup layout (tempo_popup.xml):

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <EditText
        android:id="@+id/editText1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:inputType="number" >

        <requestFocus />
    </EditText>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:layout_weight="1" >

            <Button
                android:id="@+id/confirmtempo"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:text="@string/Confirm" />

        </LinearLayout>
        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:layout_weight="1" >

            <Button
                android:id="@+id/canceltempo"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:text="@string/Cancel" />
         </LinearLayout>
    </LinearLayout>
</LinearLayout>

Java code:

 public class MetronomeActivity extends Activity{
      @Override
      public void onCreate(Bundle savedInstanceState) {
           super.onCreate(savedInstanceState);
           setContentView(R.layout.activity_metronome);
           final Button plus = (Button) findViewById(R.id.tempop);
           final Button minus = (Button) findViewById(R.id.tempom);
           final Button confirm_tempo = (Button) findViewById(R.id.confirmtempo);

           plus.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                     final LayoutInflater inflater = (LayoutInflater) getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                     View pop = inflater.inflate(R.layout.tempo_popup, null, false); 
                     final PopupWindow pw = new PopupWindow(
                     inflater.inflate(R.layout.tempo_popup, null, false), 
                                      250, 150, true);

                     // The code below assumes that the root container has an id called 'main'
                     pw.showAtLocation(plus, Gravity.CENTER, 0, 0);
                     Button cancel_tempo = (Button) pop.findViewById(R.id.canceltempo);

                     cancel_tempo.setOnClickListener( new View.OnClickListener() {
                                         @Override
                                         public void onClick(View v) {
                                              Log.i("Begin1", "POPUP CANCEL");
                                              pw.dismiss();
                                         }
                     });
                }
           });
      }
 }

Upvotes: 1

Views: 1137

Answers (2)

RobinHood
RobinHood

Reputation: 10969

Damn it! Replace single line of code, it will work like charm.

Instead:

PopupWindow popUpWindow = new PopupWindow(inflater.inflate(R.layout.tempo_popup, null, false),250, 150, true);

Use:

View view = inflater.inflate(R.layout.tempo_popup, null, false);
PopupWindow popUpWindow = new PopupWindow(view, 250, 150,  true);

Upvotes: 2

Sunkas
Sunkas

Reputation: 9590

Does the Popup cancel log get printed? Otherwise this might help:

Android popup window dismissal

Upvotes: 0

Related Questions