Android IME: how to display popup dialog?

I play with keyboard design and try to show a popup 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 part CONTEXT. In a regular application, it will be simple this. I also tried getApplicationContext()and getBaseContext(), but none of these work -> keyboard crash.

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

So, I am wondering if I need to do something with InputConnection :

The InputConnection interface is a communication channel from InputMethod back to the application that receives its input. this is used to accomplish such things as reading text around the cursor, fixing text in the text box, and sending raw key events to the application.

Until now, I could not figure out how to do this. I definitely know that this is possible since I saw it before. I could point me in the right direction, which would definitely be appreciated.


Update:

To provide a better idea of ​​what I'm trying to achieve, I downloaded a screenshot of the Swype keyboard, which does just that: displaying a popup when a special key is pressed on the keyboard.

Swype pop-up dialog

+5
source share
5 answers

, ,

:

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();

.

+4

IME , . , , :

http://developer.android.com/reference/android/view/WindowManager.LayoutParams.html#TYPE_APPLICATION_PANEL

WindowManager.LayoutParams.token IME ( View.getWindowToken()).

, , . IME, .

+3

,

android:theme="@android:style/Theme.Dialog"
+2
source

its very simple, just create activity, as here (without any representation), write the dialogue code in it

public class dialog extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setMessage("Are you sure you want to Delete All Contacts?");
    builder.setCancelable(false);

    builder.setPositiveButton("Yes",
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {
                            //deleteAllContacts();
                        }//
                    });

            builder.setNegativeButton("No",
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {
                            dialog.cancel();
                        }
                    });

    AlertDialog alert = builder.create();
    alert.show();

}
}

now go to the android manifest file and add activity like

<activity android:name=".dialog" android:theme="@android:style/Theme.NoDisplay">        </activity>

everyone enjoy

+1
source

You should get a link to your activity context. In any case, you should use the showDialog method for Activity.

0
source

All Articles