5

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

starblue
  • 55,348
  • 14
  • 97
  • 151
znq
  • 44,613
  • 41
  • 116
  • 144

4 Answers4

2

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.

Rich Schuler
  • 41,814
  • 6
  • 72
  • 59
  • Cool. Thanks for the hint. I'm already going through the Android's keyboard source code, so I'm quite used to that :-) – znq Aug 18 '10 at 17:51
  • @znq, did you actually manage to do this? Did you end up using a `PopupWindow` or something else? Could you provide pointers? – Johan Walles Sep 30 '15 at 04:44
  • It's critical to create the PopupWindow in your KeyboardView subclass, NOT in the InputMethodService or anywhere else. Use a negative Y position if you want it to appear above the keyboard. – Barry Fruitman Oct 02 '15 at 21:38
  • 1
    I figured it out; you just have to call PopupWindow.setClippingEnabled(false) before using your popup window, otherwise your negative Y coordinates won't work. Added as an answer below. – Johan Walles Jan 08 '16 at 10:41
1

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.

Johan Walles
  • 1,447
  • 2
  • 15
  • 23
  • Not working in Android P, even the Y coordinate is negative. Unless, we set the window layout type to TYPE_APPLICATION_OVERLAY, which needs SYSTEM_ALERT permission. – elsennov Mar 21 '19 at 04:27
  • @elsennov I have an app where I'm doing this. My phone is on Android P, and this works for me: https://github.com/walles/exactype/blob/2d0f3175012369bc50bc7a992a9d03de39db363e/app/src/main/java/com/gmail/walles/johan/exactype/Exactype.java#L351 – Johan Walles Mar 25 '19 at 14:32
  • Sorry, didn't mention it earlier. I meant, not all android P. I tested on my device, samsung S9, it works. But when I tested it on emulator (google pixel, android P) it did not work @JohanWalles – elsennov Mar 28 '19 at 06:13
  • I have only tested it on an S9, not in the emulator. If it doesn't work there then I don't know, sorry. – Johan Walles Mar 28 '19 at 14:50
1

Peace be upon those who follow the guidance,

solution :

AlertDialog dialog;

//add this to your code
dialog = builder.create();
Window window = dialog.getWindow(); 

WindowManager.LayoutParams lp = window.getAttributes();
lp.token = mInputView.getWindowToken();
lp.type = WindowManager.LayoutParams.TYPE_APPLICATION_ATTACHED_DIALOG;

window.setAttributes(lp);
window.addFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
//end addons

dialog.show();

===== UPDATE 30.09.2015 mInputView its the general name of your keyboard class ..see

@Override
    public View onCreateInputView() {
        mInputView =(MyKeyboardView) getLayoutInflater().inflate( R.layout.input, null);
....
}

More info : http://developer.android.com/guide/topics/text/creating-input-method.html

good luck.

Maher Abuthraa
  • 17,493
  • 11
  • 81
  • 103
1

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

Barry Fruitman
  • 12,316
  • 13
  • 72
  • 135