Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android IME: showing a custom pop-up dialog (like Swype keyboard) which can enter text into the TextView

I'm wondering how I can create a custom pop-up like the one in the screenshot below (borrowed from the Swype keyboard), where I can have a couple of buttons, which each commit a string to the currently "connected" TextView (via a InputConnection).

Please note: this is an InputMethodService and not an ordinary Activity. I already tried launching a separate Activity with Theme:Dialog. However, as soon as that one opens I lose my focus with the TextView and my keyboard disappears (and with that my InputConnection is gone).

Swype

like image 259
znq Avatar asked Aug 18 '10 16:08

znq


3 Answers

You can try using a PopupWindow. You'll have to do a bit of hacking to get it to do what you want and the only good documentation for it is the source.

like image 58
Rich Schuler Avatar answered Nov 10 '22 13:11

Rich Schuler


I was banging my head against this problem too and I finally figured it out. The above solutions are correct although as you pointed out they cannot be used from an InputMethodService because it is not an Activity. The trick is to create the PopupWindow in a subclass of KeyboardView. By using a negative Y position, the PopupWindow can appear above the keyboard like Swype.

Good luck, Barry

like image 1
Barry Fruitman Avatar answered Nov 10 '22 14:11

Barry Fruitman


Correct answer:

  1. Create a PopupWindow and put your view inside it
  2. Call popupWindow.setClippingEnabled(false)
  3. Call [popupWindow.showAtLocation()](http://developer.android.com/reference/android/widget/PopupWindow.html#showAtLocation(android.view.View, int, int, int)) with a negative Y coordinate.

This will show your popup above the IME as in your screenshot.

like image 1
Johan Walles Avatar answered Nov 10 '22 13:11

Johan Walles