0

Please help me out here.

I have set in my main activity a popup window when i press on the first button. It worked fine and i could close it both via a close button i have set up and if i wanted to touch on the outer side as well.

But I needed multiple popup windows in that same activity. I have various buttons and if you press them they should give you more info through that popup window. Now i have created my second button and it works when i open the popup window.

Here is the problem. When i close it now, i have to press either two times outside the popup window or one time outside popup window and then press the close button.. Only then i will return to my menu screen. So it doesn't matter which one of the two buttons i press, both buttons open two popup windows. It is really annoying and I hope someone smarter than me can help me out.

This is my menu.java

package com.fadi.enumbers;

import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.PopupWindow;

public class menu extends Activity {
    Button btnClosePopup;
    Button btnCreatePopup;
    Button btnCreatePopup2;
    Bitmap bitmap;
    Resources res;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btnCreatePopup = (Button) findViewById(R.id.btn_e100);
        btnCreatePopup2 = (Button) findViewById(R.id.btn_e100ii);
        btnCreatePopup.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                initiatePopupWindow();
            } 
        });

        btnCreatePopup2.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                initiatePopupWindow();
            } 
        });

    }

    private PopupWindow pwindo;
    private PopupWindow pwindo2;

    private void initiatePopupWindow() {
        try {
            // We need to get the instance of the LayoutInflater
            LayoutInflater inflater = (LayoutInflater) menu.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View layout = inflater.inflate(R.layout.screen_popup,(ViewGroup) findViewById(R.id.popup_element));

            pwindo = new PopupWindow(layout, 370, 450, true);
            pwindo.setBackgroundDrawable(new BitmapDrawable(res, bitmap));
            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();
        }

        try {
            // We need to get the instance of the LayoutInflater
            LayoutInflater inflater = (LayoutInflater) menu.this        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View layout2 = inflater.inflate(R.layout.screen_popup2, (ViewGroup)findViewById(R.id.popup_element));
            pwindo2 = new PopupWindow(layout2, 370, 450, true);
            pwindo2.setBackgroundDrawable(new BitmapDrawable(res, bitmap));
            pwindo2.showAtLocation(layout2, Gravity.CENTER, 0, 0);

            btnClosePopup = (Button) layout2.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();
        }
    };    
}
Michael
  • 3,982
  • 4
  • 30
  • 46
mido_sama
  • 1
  • 1

1 Answers1

0

In your initiatePopupWindow() you are creating two popup dialogs. Thats why its behaving like that.

Do something like passing an int value to differentiate from which button you are calling the initiatePopupWindow() method like

btnCreatePopup.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                initiatePopupWindow(1);
            } 
        });

        btnCreatePopup2.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                initiatePopupWindow(2);
            } 
        });

and then

private void initiatePopupWindow(int popupNo) {
swich(popupNo)
{
case 1:
        try {
            // We need to get the instance of the LayoutInflater
            LayoutInflater inflater = (LayoutInflater) menu.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View layout = inflater.inflate(R.layout.screen_popup,(ViewGroup) findViewById(R.id.popup_element));

            pwindo = new PopupWindow(layout, 370, 450, true);
            pwindo.setBackgroundDrawable(new BitmapDrawable(res, bitmap));
            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();
        }
break;
case 2:

        try {
            // We need to get the instance of the LayoutInflater
            LayoutInflater inflater = (LayoutInflater) menu.this        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View layout2 = inflater.inflate(R.layout.screen_popup2, (ViewGroup)findViewById(R.id.popup_element));
            pwindo2 = new PopupWindow(layout2, 370, 450, true);
            pwindo2.setBackgroundDrawable(new BitmapDrawable(res, bitmap));
            pwindo2.showAtLocation(layout2, Gravity.CENTER, 0, 0);

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

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

Use separate cancel listeners for those two buttons are customise that listener to work for both the dialogs.

Priya
  • 489
  • 1
  • 10
  • 20