0

I created a class with a button that shows a popup defined in a library. The popup has a close button. When I tap on the close button it tries to execute dismiss method but It does not work. I would like to know why this method (dismiss) is not working.

MainActivity.java

package com.personal.testfeedback;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RelativeLayout;

import com.example.testfeedback.R;
import com.personal.feedback.Feedback;

public class MainActivity extends Activity {
    Button btnCreatePopup;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final RelativeLayout parent = (RelativeLayout)findViewById(R.id.layout);

        btnCreatePopup = (Button) findViewById(R.id.button1);
        btnCreatePopup.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                Feedback feedback = new Feedback(MainActivity.this);
                feedback.initiatePopupWindow(parent);
            }
        });

    }

}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/layout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:background="#FFF000" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button" />

</RelativeLayout>

Feedback.java

package com.personal.feedback;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;

public class Feedback extends Activity {
    private Context context;

    Button btnClosePopup;
    protected PopupWindow pwindo;

    public Feedback(Context ctx) {
        this.context = ctx;
    }


    public void initiatePopupWindow(RelativeLayout parent) {
        try {
            // We need to get the instance of the LayoutInflater
            LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

            View layout = inflater.inflate(R.layout.screen_popup, parent);

            pwindo = new PopupWindow(layout, 600, 500, true);
            pwindo.setFocusable(true);

            // pwindo.showAtLocation(layout, Gravity.CENTER, 0, 0);

            btnClosePopup = (Button) layout.findViewById(R.id.btn_close_popup);
            btnClosePopup.setOnClickListener(cancel_button_click_listener);

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

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

            pwindo.dismiss();
            return ;
        }
    };
}

screen_popup.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/popup_element"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#444444"
    android:orientation="vertical"
    android:padding="10sp" 
    >

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical" 
        android:layout_gravity="center_horizontal|center_vertical">

    <TextView
        android:id="@+id/txtView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="5sp"
        android:text="Hello!" />

    <Button
        android:id="@+id/btn_close_popup"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="Close" />
    </LinearLayout>

</RelativeLayout>

If I do not comment line: pwindo.showAtLocation(layout, Gravity.CENTER, 0, 0); the following error appears:

01-14 16:27:28.149: W/System.err(5515): java.lang.RuntimeException: view android.widget.RelativeLayout@405b49d0 being added, but it already has a parent
01-14 16:27:28.149: W/System.err(5515):     at android.view.View.assignParent(View.java:6064)
01-14 16:27:28.159: W/System.err(5515):     at android.view.ViewRoot.setView(ViewRoot.java:573)
01-14 16:27:28.169: W/System.err(5515):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:177)
01-14 16:27:28.169: W/System.err(5515):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
01-14 16:27:28.169: W/System.err(5515):     at android.view.Window$LocalWindowManager.addView(Window.java:424)
01-14 16:27:28.179: W/System.err(5515):     at android.widget.PopupWindow.invokePopup(PopupWindow.java:907)
01-14 16:27:28.179: W/System.err(5515):     at android.widget.PopupWindow.showAtLocation(PopupWindow.java:767)
01-14 16:27:28.179: W/System.err(5515):     at com.personal.feedback.Feedback.initiatePopupWindow(Feedback.java:35)
01-14 16:27:28.179: W/System.err(5515):     at com.personal.testfeedback.MainActivity$1.onClick(MainActivity.java:30)
01-14 16:27:28.179: W/System.err(5515):     at android.view.View.performClick(View.java:2494)
01-14 16:27:28.179: W/System.err(5515):     at android.view.View$PerformClick.run(View.java:9109)
01-14 16:27:28.179: W/System.err(5515):     at android.os.Handler.handleCallback(Handler.java:587)
01-14 16:27:28.179: W/System.err(5515):     at android.os.Handler.dispatchMessage(Handler.java:92)
01-14 16:27:28.189: W/System.err(5515):     at android.os.Looper.loop(Looper.java:130)
01-14 16:27:28.189: W/System.err(5515):     at android.app.ActivityThread.main(ActivityThread.java:3683)
01-14 16:27:28.189: W/System.err(5515):     at java.lang.reflect.Method.invokeNative(Native Method)
01-14 16:27:28.189: W/System.err(5515):     at java.lang.reflect.Method.invoke(Method.java:507)
01-14 16:27:28.189: W/System.err(5515):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:895)
01-14 16:27:28.189: W/System.err(5515):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:653)
01-14 16:27:28.189: W/System.err(5515):     at dalvik.system.NativeStart.main(Native Method)
01-14 16:27:45.739: D/AndroidRuntime(5515): Shutting down VM
01-14 16:27:45.739: E/AndroidRuntime(5515): Uncaught handler: thread main exiting due to uncaught exception
01-14 16:27:45.739: W/dalvikvm(5515): threadid=1: thread exiting with uncaught exception (group=0x400ad560)
01-14 16:27:45.749: E/Monkey(5515): exception :java.io.FileNotFoundException: /dev/kmsg (Permission denied)
01-14 16:27:45.779: W/System.err(5515): java.io.IOException: Permission denied
01-14 16:27:45.809: E/AndroidRuntime(5515): exception :java.io.FileNotFoundException: /dev/kmsg (Permission denied)
01-14 16:27:45.809: E/AndroidRuntime(5515): FATAL EXCEPTION: main
01-14 16:27:45.809: E/AndroidRuntime(5515): java.lang.ClassCastException: android.view.WindowManager$LayoutParams
01-14 16:27:45.809: E/AndroidRuntime(5515):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3270)
01-14 16:27:45.809: E/AndroidRuntime(5515):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
01-14 16:27:45.809: E/AndroidRuntime(5515):     at android.view.View.measure(View.java:8342)
01-14 16:27:45.809: E/AndroidRuntime(5515):     at android.widget.LinearLayout.measureVertical(LinearLayout.java:531)
01-14 16:27:45.809: E/AndroidRuntime(5515):     at android.widget.LinearLayout.onMeasure(LinearLayout.java:309)
01-14 16:27:45.809: E/AndroidRuntime(5515):     at android.view.View.measure(View.java:8342)
01-14 16:27:45.809: E/AndroidRuntime(5515):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3279)
01-14 16:27:45.809: E/AndroidRuntime(5515):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
01-14 16:27:45.809: E/AndroidRuntime(5515):     at android.view.View.measure(View.java:8342)
01-14 16:27:45.809: E/AndroidRuntime(5515):     at android.view.ViewRoot.performTraversals(ViewRoot.java:841)
01-14 16:27:45.809: E/AndroidRuntime(5515):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1876)
01-14 16:27:45.809: E/AndroidRuntime(5515):     at android.os.Handler.dispatchMessage(Handler.java:99)
01-14 16:27:45.809: E/AndroidRuntime(5515):     at android.os.Looper.loop(Looper.java:130)
01-14 16:27:45.809: E/AndroidRuntime(5515):     at android.app.ActivityThread.main(ActivityThread.java:3683)
01-14 16:27:45.809: E/AndroidRuntime(5515):     at java.lang.reflect.Method.invokeNative(Native Method)
01-14 16:27:45.809: E/AndroidRuntime(5515):     at java.lang.reflect.Method.invoke(Method.java:507)
01-14 16:27:45.809: E/AndroidRuntime(5515):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:895)
01-14 16:27:45.809: E/AndroidRuntime(5515):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:653)
01-14 16:27:45.809: E/AndroidRuntime(5515):     at dalvik.system.NativeStart.main(Native Method)
01-14 16:27:45.829: E/AndroidRuntime(5515): exception :java.io.FileNotFoundException: /dev/kmsg (Permission denied)
01-14 16:27:45.839: W/System.err(5515): java.io.FileNotFoundException: /data/plog.log (Permission denied)

Regards, Alek

alekandro
  • 11
  • 1
  • 4
  • Is there a reason for the `Feedback` class extending `Activity`? – user Jan 14 '14 at 18:15
  • There is no reason, but it doesn't change the functionality, dismiss() still not working. – alekandro Jan 14 '14 at 18:30
  • Out of curiosity, can you try `View layout = inflater.inflate(R.layout.screen_popup, null);`? – user Jan 14 '14 at 18:32
  • using `View layout = inflater.inflate(R.layout.screen_popup, null);` never shows the popup. No error in LogCat but I don't see the popup. – alekandro Jan 14 '14 at 18:38
  • Did you also uncommented the line `pwindo.showAtLocation(parent, Gravity.CENTER, 0, 0);`? – user Jan 14 '14 at 18:45
  • It breaks with that line. Perhaps this helps, when it was all the code in the main activity, it worked. Problem is in the library, it seems I am messing up with the contexts, but I can't fix it – alekandro Jan 14 '14 at 18:50
  • It breaks meaning a crash? If yes post the exception. – user Jan 14 '14 at 19:00
  • the following is the error that throws when executing `pwindo.showAtLocation(layout, Gravity.CENTER, 0, 0);`: – alekandro Jan 14 '14 at 19:08
  • I modified the question adding the StackTrace – alekandro Jan 14 '14 at 19:16
  • Try cleaning your project and running it again. With the change I've said above your code works with no problems. – user Jan 14 '14 at 19:33
  • The same error. Cleaning the project did not help. – alekandro Jan 14 '14 at 19:41
  • I've run your exact code(with the single change I said above about the inflate row with a second null parameter) and it works, at least on a 4.4 device. – user Jan 14 '14 at 19:45
  • Yes It worked. inflating like this: `View layout = inflater.inflate(R.layout.screen_popup, null);` and adding the commented row: `pwindo.showAtLocation(layout, Gravity.CENTER, 0, 0);` – alekandro Jan 14 '14 at 19:51

1 Answers1

0

You mixed up things in your code.

Firstly, I also don't understand why your Feedback class extends Activity. It is an helper class and has nothing to do with an Activity.

Secondly, your error when decommenting pwindo.showAtLocation is due to the fact that your call to inflater.inflate(R.layout.screen_popup, parent) gives the RelativeLayout present in screen_popup a parent, so with showAtLocation you are trying to assign a parent again to the same resource.

Coming to your question, your way to open the popup is a little strange. Infacts you're are passing the context of the MainActivity and are inflating the new layout. So, what you see on the GUI is not a new window on top of the previous one, but it is the previous one with different layout. You can see that by pressing the back button on your Android device when the popup is displayed: you are not moving to the previous screen, but you are simply exiting the application.

So, when you create the PopupWindow, you are not using it (because you didn't call its showAtLocation method), so the call to its dismiss() method provides with no effects. To close the window you have to call the finish() method on the context (you have to cast it to MainActivity):

((MainActivity)context).finish()

But I know that this is not you're trying to do, as it doesn't close the popup, but the entire aplication. To have a popup effect, try this cods:

public class Feedback {
   private Context context;
   private MainActivity parent;

   Button btnClosePopup;
   protected PopupWindow pwindo;

   public Feedback(MainActivity _parent) {
      parent = _parent;
   }

   public void initiatePopupWindow() {

      try {
         // We need to get the instance of the LayoutInflater
         RelativeLayout viewGroup = (RelativeLayout) parent
               .findViewById(R.id.popup_element);
     LayoutInflater inflater = (LayoutInflater) parent
           .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

     View layout = inflater.inflate(R.layout.screen_popup, viewGroup);

     pwindo = new PopupWindow(layout, 600, 500, true);
     pwindo.setFocusable(true);

     pwindo.showAtLocation(layout, Gravity.CENTER, 0, 0);

     btnClosePopup = (Button) layout.findViewById(R.id.btn_close_popup);
     btnClosePopup.setOnClickListener(cancel_button_click_listener);

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


   private OnClickListener cancel_button_click_listener = new OnClickListener() 

      public void onClick(View v) {

         pwindo.dismiss();
         return;
      }
   };
}
sthor69
  • 638
  • 1
  • 10
  • 25