Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android popup window dismissal

I have a popup window displaying when I click an item in my list activity. The problem is that the back key doesn't close it. I tried catching the back key in my list activity but it doesn't register it...then I tried registering a onkeylistener to the view I'm passing to my popup window. Like this:

pop.setOnKeyListener(new View.OnKeyListener() {          @Override         public boolean onKey(View v, int keyCode, KeyEvent event) {             // TODO Auto-generated method stub             boolean res=false;             if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {                 // do something on back.                 Log.e("keydown","back");                 if (pw.isShowing()) {                     Log.e("keydown","pw showing");                     pw.dismiss();                     res = true;                 }             } else {                 res = false;             }             return res;         }     }); 

which is passed to a popup like this:

pw = new PopupWindow(        pop,         240,         70,         true); 

But that listener doesn't fire neither. Can you help me? I'm out of ideas :)

like image 734
Bostjan Avatar asked Jun 25 '10 20:06

Bostjan


People also ask

How do you dismiss a popup?

just write like this below: popupWindow. setOutsideTouchable(true); popupWindow. setCancelable(true);

What is popup window in Android?

android.widget.PopupWindow. This class represents a popup window that can be used to display an arbitrary view. The popup window is a floating container that appears on top of the current activity.


2 Answers

This is because the popup window does not respond to onTouch or onKey events unless it has a background that != null. Check out some code I wrote to help with this. In the basic case you can to call PopupWindow#setBackgroundDrawable(new BitmapDrawable()) to force it to act the way you expect. You won't need your own onKey listener. You might also need to call PopupWindow#setOutsideTouchable(true) if you want it to go away when the user clicks outside of the window boundaries.

Extended esoteric answer:

The reason the background cannot be null is because of what happens in PopupWindow#preparePopup. If it detects background != null it creates an instance of PopupViewContainer and calls setBackgroundDrawable on that and puts your content view in it. PopupViewContainer is basically a FrameLayout that listens for touch events and the KeyEvent.KEYCODE_BACK event to dismiss the window. If background == null, it doesn't do any of that and just uses your content view. You can, as an alternative to depending on PopupWindow to handle that, extend your root ViewGroup to behave the way you want.

like image 189
Rich Schuler Avatar answered Oct 04 '22 10:10

Rich Schuler


Do as per following it works fine:

PopupWindow pw; LayoutInflater inflater = (LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE); View layout = inflater.inflate(R.layout.weight_popup, (ViewGroup)findViewById(R.id.linlay_weight_popup)); pw = new PopupWindow(layout,LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT, true); pw.setBackgroundDrawable(new BitmapDrawable()); pw.setOutsideTouchable(true); pw.showAsDropDown(btnSelectWeight); 
like image 24
Sunil Parmar Avatar answered Oct 04 '22 10:10

Sunil Parmar