Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android IME: how to show a pop-up dialog?

I'm playing around with some keyboard development and try to show a pop-up dialog when a certain key is pressed

if (primaryCode == -301) {
            AlertDialog mDialog = new AlertDialog.Builder(CONTEXT)
            .setTitle("My dialog")
            .setMessage("Lets do it.")
            .setPositiveButton("ok", null).create();
             mDialog.show();
}

However, the problem is the CONTEXT part. In a normal application it would just be this. I also tried getApplicationContext() and getBaseContext(), but neither of those works -> keyboard crashes.

android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application

So I'm wondering if I have to do something with InputConnection:

The InputConnection interface is the communication channel from an InputMethod back to the application that is receiving its input. It is used to perform such things as reading text around the cursor, committing text to the text box, and sending raw key events to the application.

So far I wasn't able to figure out how. I definitely know it's possible, since I have seen it before. I someone could point me in the right direction that would definitely be appreciated.


Update:

To provide a better picture of what I try to achieve I uploaded a screenshot of the Swype keyboard, which does exactly that: showing a pop-up dialog when a special key gets pressed on the keyboard.

Swype pop-up dialog

like image 294
znq Avatar asked Aug 16 '10 15:08

znq


1 Answers

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
alert.show();

good luck.

like image 87
Maher Abuthraa Avatar answered Sep 17 '22 04:09

Maher Abuthraa