title: Android IME: how to show a pop-up dialog?
body: <p>I'm playing around with some keyboard development and try to show a pop-up dialog when a certain key is pressed</p>
  
  <pre><code>if (primaryCode == -301) {
              AlertDialog mDialog = new AlertDialog.Builder(CONTEXT)
              .setTitle("My dialog")
              .setMessage("Lets do it.")
              .setPositiveButton("ok", null).create();
               mDialog.show();
  }
  </code></pre>
  
  <p>However, the problem is the <code>CONTEXT</code> part. In a normal application it would just be <code>this</code>. I also tried <code>getApplicationContext()</code> and <code>getBaseContext()</code>, but neither of those works -> keyboard crashes.</p>
  
  <blockquote>
    <p>android.view.WindowManager$BadTokenException:
    Unable to add window -- token null is
    not for an application</p>
  </blockquote>
  
  <p>So I'm wondering if I have to do something with <a href="http://developer.android.com/reference/android/view/inputmethod/InputConnection.html" rel="noreferrer">InputConnection</a>:</p>
  
  <blockquote>
    <p>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.</p>
  </blockquote>
  
  <p>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.</p>
  
  <hr>
  
  <p><strong>Update:</strong></p>
  
  <p>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.</p>
  
  <p><img src="https://i.stack.imgur.com/r6Dro.png" alt="Swype pop-up dialog"></p>