Tuesday, October 18, 2011

Android development: ListView inside a PopupWindow

For some reason (you don't want to know believe me), I had to put a ListView in a PopupWindow. In more recent versions of Android (newer than Gingerbread) there is the convenient android.widget.ListPopupWindow class which does that job for you.
But for Gingerbread, you'll have to put the list in the PopupWindow. That's what I did, it showed up fine when I show the popup, I could scroll the ListView, but sadness, the OnItemClick of the OnItemClickListener would not get fired when pressing a item in the ListView.
After spending way to much time (getting the Android Java source code associated with the Android jar in Eclipse and stepping through the code in the debugger), I discovered that I had to set the PopupWindow to be focusable:
mPopupWindow.setFocusable(true);
And then voila, the OnItemClick gets called!
Hope it helps someone!

2 comments:

  1. I have no words for this great post such a awe-some information i got gathered. Thanks to Author.
    Vee Eee Technologies

    ReplyDelete
  2. Thanks, this looks great to me, had the same problem but....
    where do I set mPopupWindow.setFocusable(true);

    ???

    Thanks Lou

    ReplyDelete