Lucy
Lucy

Reputation: 43

Open PopupWindow in PopupWindow

I want to open a PopUpWindow in another PopUpWindow. I have an ImageButtons in my MainActivity. When I click on it a PopUpWindow appears. I use it as a kind of submenu in my app. In my first PopupWindow is another ImageButton. If I click on it a second PopupWindow should appear and overlay the first one.

Opening the first PopupWindow works just fine. When I click on the button in it to open the second one, the app crashes. How can I make the second PopupWindow work?

Thanks for your help.

I tried it likes this:

final ImageButton btnOpenPopup = (ImageButton) findViewById(R.id.button_name);
        btnOpenPopup.setOnClickListener(new Button.OnClickListener() {

            @Override
            public void onClick(View v) {

                LayoutInflater layoutInflater
                        = (LayoutInflater) getBaseContext()
                        .getSystemService(LAYOUT_INFLATER_SERVICE);

                View popupView = layoutInflater.inflate(R.layout.popup_fertig, null);

                final PopupWindow popupWindow = new PopupWindow(
                        popupView,
                        LayoutParams.WRAP_CONTENT,
                        LayoutParams.WRAP_CONTENT);

           popupWindow.showAtLocation(btnOpenPopup, Gravity.TOP | Gravity.RIGHT, 0, 0);



           Button btn_2 = (Button) popupView.findViewById(R.id.button_2);
           btn_2.setOnClickListener(new Button.OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                     LayoutInflater layoutInflater_2
                        = (LayoutInflater) getBaseContext()
                        .getSystemService(LAYOUT_INFLATER_SERVICE);

                View popupView_2 = layoutInflater.inflate(R.layout.popup_2, null);

                final PopupWindow popupWindow_2 = new PopupWindow(
                        popupView_2,
                        LayoutParams.WRAP_CONTENT,
                        LayoutParams.WRAP_CONTENT);

           popupWindow_2.showAtLocation(btn_2, Gravity.TOP | Gravity.RIGHT, 0, 0);


               }
                }
            });
}

Upvotes: 1

Views: 67

Answers (1)

Simon
Simon

Reputation: 19928

I'm also trying to do what you are doing but was not successful however I did figure out a workaround for what you want to do.

Inside your popupView xml layout, you would have to create a framelayout as the parent layout and then put both your popupView and popupView 2 layout within the parent layout. You would then switch on and off the visibility for each of the two layouts when a button is pressed in the popupView.

It actually works quite nicely, the popupView resizes itself appropriately according to the content it holds with smooth animations.

Upvotes: 1

Related Questions