1

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?

Philipp
  • 11,549
  • 8
  • 66
  • 126
  • Start a dialog-themed activity. – CommonsWare Aug 18 '18 at 11:14
  • @commonsware I am considering this, but how would you return data to the IME service? I can't use startActivityForResult from my service. – Philipp Aug 18 '18 at 11:19
  • If the activity and the service are in the same process, you would use whatever you want. That could be an event bus (e.g., `LocalBroadcastManager`). That could be by both interacting with a singleton repository. And so on. If the activity and service are in different processes, use a `PendingIntent`, or a `Messenger`, or a `ResultReceiver`, etc. – CommonsWare Aug 18 '18 at 11:27
  • @CommonsWare I have implemented the activity as a dialog (with theme=) and this works in most cases, but users have pointed out that on Chrome the IME is closed when the dialog pops up and it seems like I cannot input the text to the input field after that anymore. I am using getWindow().addFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM); in the activity's onCreate. Any thoughts? Thanks. – Philipp Aug 23 '18 at 06:02
  • 1
    "users have pointed out that on Chrome the IME is closed when the dialog pops up" -- you don't have control over that. Whether the IME is showing is up to the app that has the `EditText` (or whatever) that triggers the IME. In this case, Chrome may be using `InputMethodManager` to hide the IME when Chrome no longer has the input focus. – CommonsWare Aug 23 '18 at 10:44
  • But it doesn't do this with an AlertDialog. It's a shame that the previous way does no longer work. – Philipp Aug 23 '18 at 10:52
  • @CommonsWare slightly related, but do you know if it is possible to start a dialog-themed activity from the IME without causing the IME close? The best I've managed is for it to be closed/opened again with "android:windowSoftInputMode". – user2891659 Oct 16 '18 at 14:49
  • @user2891659: No clue, sorry! – CommonsWare Oct 16 '18 at 21:46
  • I found that both PopupWindow and Alertdialog show only inside the candidateview and keyboardview. – osfans Oct 29 '18 at 07:56

2 Answers2

1

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

osfans
  • 92
  • 7
  • Looking into the detailed changes here and making the change from LayoutParams.TYPE_APPLICATION_ATTACHED_DIALOG to instead use LayoutParams.TYPE_APPLICATION_OVERLAY worked for me. But you do have to add Manifest.permission.SYSTEM_ALERT_WINDOW to your manifest (and the permission granted) per the documentation. – David White Apr 18 '20 at 18:06
  • E/AndroidRuntime: FATAL EXCEPTION: IntentService[BTDeviceMaintainerService] Process: com.unionpay.smartpos.service, PID: 3926 android.view.WindowManager$BadTokenException: Unable to add window -- token null is not valid; is your activity running? – linjiejun Apr 22 '20 at 06:07
0

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.

  • Here's an IME: https://github.com/BrainBear/LatinIME. It is ported from AOSP LatinIME. AlertDialog works well when long click comma. – osfans Dec 11 '18 at 07:39