title: Want custom keyboard to ONLY be used for MY APP and restore previous when app loses focus
body: <p>I made this "custom keyboard" by following the wonderful outline in <a href="http://code.tutsplus.com/tutorials/create-a-custom-keyboard-on-android--cms-22615" rel="nofollow noreferrer">this link</a> AND NOT USING ECLIPSE. I used Android Studio (<strong>AS</strong>) 1.1.0.</p>
  
  <p>Here's a screenshot from my device:</p>
  
  <p><img src="https://i.stack.imgur.com/U3LbF.png" alt="enter image description here"></p>
  
  <p>The only problem is that the procedure requires <strong>changing settings for <code>Language and Input</code> AND ALSO replaces the keyboard FOR ALL APPS</strong>. I don't want that. I just want MY app to change the keyboard for ITSELF and then revert to the previous keyboard as soon as my app goes off screen, otherwise I'm going to be a huge pain in users' butts. Rather than do that, I may as well just add buttons to perform the keypresses I hoped to invoke via a custom keyboard. (It's not gruesome; user just has to pull down the notification bar and select choose input method, but still too intrusive for most users.)</p>
  
  <p>Here's the structure:</p>
  
  <p><img src="https://i.stack.imgur.com/Qx8a3.png" alt="enter image description here"></p>
  
  <p>The keypad is altered in <code>qwerty.xml</code> by doing lots of this:</p>
  
  <pre><code>&lt;Keyboard xmlns:android="http://schemas.android.com/apk/res/android"
            android:keyWidth="10%p"
            android:horizontalGap="0px"
            android:verticalGap="0px"
            android:keyHeight="60dp"
      &gt;
      &lt;Row&gt;
          &lt;Key android:codes="49" android:keyLabel="1" android:keyEdgeFlags="left"/&gt;
          &lt;Key android:codes="50" android:keyLabel="2"/&gt;
          &lt;Key android:codes="51" android:keyLabel="3"/&gt;
          &lt;Key android:codes="52" android:keyLabel="4"/&gt;
          &lt;Key android:codes="53" android:keyLabel="5"/&gt;
          &lt;Key android:codes="54" android:keyLabel="6"/&gt;
          &lt;Key android:codes="55" android:keyLabel="7"/&gt;
          &lt;Key android:codes="56" android:keyLabel="8"/&gt;
          &lt;Key android:codes="57" android:keyLabel="9"/&gt;
          &lt;Key android:codes="48" android:keyLabel="0" android:keyEdgeFlags="right"/&gt;
      &lt;/Row&gt;
  ...
  </code></pre>
  
  <p>Here's <code>method.xml</code>:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;input-method xmlns:android="http://schemas.android.com/apk/res/android"&gt;
      &lt;subtype
          android:label=              "@string/subtype_en_US"
          android:imeSubtypeLocale=   "en_US"
          android:imeSubtypeMode=     "keyboard" 
      /&gt;
  &lt;/input-method&gt;
  </code></pre>
  
  <p>Here's <code>AndroidManifest.xml</code>:</p>
  
  <pre><code>&lt;manifest xmlns:android="http://schemas.android.com/apk/res/android"
            package="com.dslomer64.simplekeyboard"&gt;
  
      &lt;application
  
                  android:allowBackup="true"
                  android:label="@string/app_name"
                  android:icon="@mipmap/ic_launcher"
                  android:theme="@style/AppTheme"&gt;
  
                  &lt;service android:name=".SimpleIME"
                       android:label="@string/simple_ime"
                       android:permission="android.permission.BIND_INPUT_METHOD"
                  &gt;
                  &lt;meta-data android:name="android.view.im" android:resource="@xml/method"/&gt;
                  &lt;intent-filter&gt;
                      &lt;action android:name="android.view.InputMethod" /&gt;
                  &lt;/intent-filter&gt;
          &lt;/service&gt;
  
      &lt;/application&gt;
  
  &lt;/manifest&gt;
  </code></pre>
  
  <p>The <code>service</code> that's added to the manifest is what gets at the soft keyboard through the code in <code>SimpleIME.java</code> (empty overrides omitted):</p>
  
  <pre><code>import android.inputmethodservice.InputMethodService;
  import android.inputmethodservice.Keyboard;
  import android.inputmethodservice.KeyboardView;
  import android.media.AudioManager;
  import android.view.KeyEvent;
  import android.view.View;
  import android.view.inputmethod.InputConnection;
  
  public class SimpleIME extends InputMethodService
      implements KeyboardView.OnKeyboardActionListener
  {
  
    private KeyboardView kv;
    private Keyboard keyboard;
  
    private boolean caps = false;
  
    @Override
    public View onCreateInputView() {
      kv = (KeyboardView)getLayoutInflater().inflate(R.layout.keyboard, null);
      keyboard = new Keyboard(this, R.xml.qwerty);
      kv.setKeyboard(keyboard);
      kv.setOnKeyboardActionListener(this);
      return kv;
    }
    private void playClick(int keyCode){
      AudioManager am = (AudioManager)getSystemService(AUDIO_SERVICE);
      switch(keyCode){
        case 32:
          am.playSoundEffect(AudioManager.FX_KEYPRESS_SPACEBAR);
          break;
        case Keyboard.KEYCODE_DONE:
        case 10:
          am.playSoundEffect(AudioManager.FX_KEYPRESS_RETURN);
          break;
        case Keyboard.KEYCODE_DELETE:
          am.playSoundEffect(AudioManager.FX_KEYPRESS_DELETE);
          break;
        default: am.playSoundEffect(AudioManager.FX_KEYPRESS_STANDARD);
      }
    }
    @Override
    public void onKey(int primaryCode, int[] keyCodes) {
      InputConnection ic = getCurrentInputConnection();
      playClick(primaryCode);
      switch(primaryCode){
        case Keyboard.KEYCODE_DELETE :
          ic.deleteSurroundingText(1, 0);
          break;
        case Keyboard.KEYCODE_SHIFT:
          caps = !caps;
          keyboard.setShifted(caps);
          kv.invalidateAllKeys();
          break;
        case Keyboard.KEYCODE_DONE:
          ic.sendKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_ENTER));
          break;
        default:
          char code = (char)primaryCode;
          if(Character.isLetter(code) &amp;&amp; caps){
            code = Character.toUpperCase(code);
          }
          ic.commitText(String.valueOf(code),1);
      }
    }
      ...
  }
  </code></pre>
  
  <p>I wonder if anyone out there has done what I need: implement a custom keyboard JUST FOR ONE APP and restore original upon loss of screen focus.</p>