Philipp
Philipp

Reputation: 11833

Display dialog from Input Method Service in Android 9 (Android Pie)

My app includes an Input Method Service with a special button that brings up a dialog. For users with Android 9, this dialog is not displayed correctly, only the part above the IME is visible:

enter image description here

The code to create the dialog is

    AlertDialog dialog = builder.create();
    Window window = dialog.getWindow();
    WindowManager.LayoutParams lp = window.getAttributes();
    LatinKeyboardView inputView = mKeyboardSwitcher.getInputView();
    lp.token = inputView.getWindowToken();
    lp.type = WindowManager.LayoutParams.TYPE_APPLICATION_ATTACHED_DIALOG;
    window.setAttributes(lp);
    window.addFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);

    dialog.show();

which is the same as described in https://stackoverflow.com/a/13962770/292233

I also tried TYPE_APPLICATION_PANEL as suggested in https://stackoverflow.com/a/3508462/292233 but this doesn't help either.

Is there any easy fix to this?

Upvotes: 1

Views: 2236

Answers (2)

osfans
osfans

Reputation: 92

The problem can be fixed by using WindowManager.LayoutParams.TYPE_APPLICATION_ATTACHED_DIALOG instead of TYPE_APPLICATION_ATTACHED_DIALOG and requesting android.permission.SYSTEM_ALERT_WINDOW.

Detailed change is here: https://github.com/osfans/trime/commit/d8e9da2dfe2653c94cd4aecba00728b7a910cbb8

Upvotes: 1

fkytestandroid
fkytestandroid

Reputation: 1

If both PopupWindow & AlertDialog do not work, I think we can use floating view, like this:

  1. Custom a view, make it look like a dialog.
  2. Use WindowManager to add the view, the view will be shown on top of any android application. I have learnt it from FlowtingExample

I tried using it. It works on Android P. However, this method get serious problem with global keys:

When "the dialog" is showing from keyboard, user press global key like Home key.

=> Home screen is shown. But, "the dialog" & keyboard is still showing.

(Is there anyway to fix this limitation?).

(I heard language selection dialog from Google keyboard (Gboard) works perfectly on Android P. But I cant find source code of Gboard. Is there anyway to get it?)

Sorry I am totally new with stackoverflow, I think I should post this into comment section but I cant do that because I do not have enough 50 reputations.

Upvotes: 0

Related Questions