title: Implementing requestFocus(View.FOCUS_DOWN) on custom keyboard in android
body: <p>Some days ago I implement custom keyboard for several EditText in my application for android. Number of these EditText may vary depend on situation. In my keyboard i want to implement "Enter" key, that would request focus to next EditText in layout (like in standart andoid soft keyboard). I'm trying to use</p>
  
  <pre><code>editText.requestFocus(View.FOCUS_DOWN)
  </code></pre>
  
  <p>but it seems this method didn't work.</p>
  
  <p>Here is my implementation of keyboard:</p>
  
  <pre><code>public class MacKeyboard implements KeyboardView.OnKeyboardActionListener {
  
      EditText mParentEditText;
      KeyboardView mKeyboardView;
  
      static final int CODE_BACKSPACE = 16;
      static final int CODE_HIDE_KEYBOARD = 17;
      static final int CODE_NEXT_INPUT = 18;
  
      static final HashMap&lt;Integer, Integer&gt; codeMap = new HashMap&lt;Integer, Integer&gt;() {{
          put(0, KeyEvent.KEYCODE_0);
          put(1, KeyEvent.KEYCODE_1);
          put(2, KeyEvent.KEYCODE_2);
          put(3, KeyEvent.KEYCODE_3);
          put(4, KeyEvent.KEYCODE_4);
          put(5, KeyEvent.KEYCODE_5);
          put(6, KeyEvent.KEYCODE_6);
          put(7, KeyEvent.KEYCODE_7);
          put(8, KeyEvent.KEYCODE_8);
          put(9, KeyEvent.KEYCODE_9);
          put(10, KeyEvent.KEYCODE_A);
          put(11, KeyEvent.KEYCODE_B);
          put(12, KeyEvent.KEYCODE_C);
          put(13, KeyEvent.KEYCODE_D);
          put(14, KeyEvent.KEYCODE_E);
          put(15, KeyEvent.KEYCODE_F);
      }};
  
      void handleBackspace() {
          mParentEditText.onKeyDown(
                  KeyEvent.KEYCODE_DEL,
                  new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_DEL)
          );
      }
  
      public MacKeyboard(KeyboardView keyboardView) {
          mKeyboardView = keyboardView;
      }
  
      public boolean isVisible() {
          return mKeyboardView.getVisibility() == View.VISIBLE;
      }
  
      public void hide() {
          mKeyboardView.setVisibility(View.GONE);
      }
  
      public void show(EditText parentEditText) {
          mKeyboardView.setVisibility(View.VISIBLE);
          mParentEditText = parentEditText;
      }
  
      public void toggle(EditText parentEditText) {
          switch ( mKeyboardView.getVisibility() ) {
              case View.VISIBLE:
                  mKeyboardView.setVisibility(View.GONE);
                  break;
  
              case View.GONE:
  
              case View.INVISIBLE:
                  mKeyboardView.setVisibility(View.VISIBLE);
                  mParentEditText = parentEditText;
                  break;
          }
      }
  
      @Override
      public void onPress(int primaryCode) {
          //do nothing
      }
  
      @Override
      public void onRelease(int primaryCode) {
          //do nothing
      }
  
      @Override
      public void onKey(int primaryCode, int[] keyCodes) {
          if ( mParentEditText != null ) {
              if ( codeMap.containsKey(primaryCode) ) {
                  int actualKeycode = codeMap.get(primaryCode);
                  if ( primaryCode &lt; 10 ) {
                      mParentEditText.onKeyDown(
                              actualKeycode,
                              new KeyEvent(KeyEvent.ACTION_DOWN, actualKeycode)
                      );
                  } else {
                      mParentEditText.onKeyDown(
                              actualKeycode,
                              new KeyEvent(0, 0, KeyEvent.ACTION_DOWN, actualKeycode, 0, KeyEvent.META_SHIFT_ON)
                      );
                  }
              } else {
                  switch ( primaryCode ) {
  
                      case CODE_BACKSPACE:
                          handleBackspace();
                          break;
  
                      case CODE_HIDE_KEYBOARD:
                          hide();
                          break;
  
                      case CODE_NEXT_INPUT:
  
                          mParentEditText.clearFocus();
                          mParentEditText.requestFocus(View.FOCUS_DOWN);
  
                         /* here i also tried 
                           mParentEditText.onKeyDown(
                              KeyEvent.KEYCODE_ENTER,
                              new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_ENTER)
                           );
  
                           and editText.post(...);
                         */
  
                  }
              }
          }
      }
  
      @Override
      public void onText(CharSequence text) {
          //do nothing
      }
  
      @Override
      public void swipeLeft() {
          handleBackspace();
      }
  
      @Override
      public void swipeRight() {
          //do nothing
      }
  
      @Override
      public void swipeDown() {
          hide();
      }
  
      @Override
      public void swipeUp() {
          //do nothing
      }
  }
  </code></pre>
  
  <p>XML keyboard layout</p>
  
  
  
  <pre><code>&lt;Keyboard
          xmlns:android="http://schemas.android.com/apk/res/android"
          android:keyWidth="20%p"
          android:keyHeight="60.0dip"
          android:horizontalGap="3.0dip"
          android:verticalGap="2.0dip"&gt;
  
      &lt;Row&gt;
          &lt;Key android:codes="1" android:keyLabel="1"/&gt;
          &lt;Key android:codes="2" android:keyLabel="2"/&gt;
          &lt;Key android:codes="3" android:keyLabel="3"/&gt;
          &lt;Key android:codes="4" android:keyLabel="4"/&gt;
          &lt;Key android:codes="5" android:keyLabel="5"/&gt;
      &lt;/Row&gt;
  
      &lt;Row&gt;
          &lt;Key android:codes="6" android:keyLabel="6"/&gt;
          &lt;Key android:codes="7" android:keyLabel="7"/&gt;
          &lt;Key android:codes="8" android:keyLabel="8"/&gt;
          &lt;Key android:codes="9" android:keyLabel="9"/&gt;
          &lt;Key android:codes="0" android:keyLabel="0"/&gt;
      &lt;/Row&gt;
  
      &lt;Row&gt;
          &lt;Key android:codes="10" android:keyLabel="A"/&gt;
          &lt;Key android:codes="11" android:keyLabel="B"/&gt;
          &lt;Key android:codes="12" android:keyLabel="C"/&gt;
          &lt;Key android:codes="13" android:keyLabel="D"/&gt;
          &lt;Key android:codes="14" android:keyLabel="E"/&gt;
          &lt;Key android:codes="15" android:keyLabel="F"/&gt;
      &lt;/Row&gt;
  
      &lt;Row android:keyWidth="33.3%p"&gt;
          &lt;Key android:codes="17" android:keyLabel="."/&gt;
          &lt;Key android:codes="18" android:keyLabel="&amp;gt;"/&gt;
          &lt;Key android:codes="16" android:keyIcon="@drawable/ic_backspace" android:isRepeatable="true"/&gt;
  
      &lt;/Row&gt;
  
  &lt;/Keyboard&gt;
  </code></pre>
  
  <p>main layout</p>
  
  
  
  <pre><code>&lt;RelativeLayout
          xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_height="match_parent"
          android:layout_width="match_parent"&gt;
  
      &lt;ScrollView
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:layout_above="@+id/keyboard_mac"
              android:fillViewport="true"
              android:clipChildren="false"
              android:clipToPadding="false"&gt;
  
          &lt;RelativeLayout
                  xmlns:android="http://schemas.android.com/apk/res/android"
                  android:id="@+id/focusDisabler"
                  android:paddingBottom="10dp"
                  android:layout_marginTop="5dp"
                  android:layout_marginLeft="10dp"
                  android:layout_marginRight="10dp"
                  android:layout_gravity="center_horizontal"
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  android:focusableInTouchMode="true"&gt;
  
              &lt;LinearLayout
                      xmlns:android="http://schemas.android.com/apk/res/android"
                      android:id="@+id/group_wlanMac"
                      android:orientation="vertical"
                      android:layout_width="match_parent"
                      android:layout_height="wrap_content"
                      android:layout_alignParentTop="true"
                      android:background="@drawable/bcg_button" &gt;
  
                  &lt;TextView
                          android:id="@+id/tview_wlanMac"
                          android:layout_width="match_parent"
                          android:layout_height="wrap_content"
                          android:layout_marginLeft="25dp"
                          android:layout_marginRight="20dp"
                          android:layout_marginTop="10dp"
                          android:layout_marginBottom="5dp"
                          android:textColor="#ffffff"
                          android:text="@string/txt_wlanMac"/&gt;
  
                  &lt;RelativeLayout
                          android:layout_height="wrap_content"
                          android:layout_width="match_parent"
                          android:layout_marginLeft="20dp"
                          android:layout_marginRight="20dp"
                          android:layout_marginBottom="10dp"&gt;
  
                      &lt;EditText
                              android:id="@+id/tedit_wlanMac"
                              android:layout_width="match_parent"
                              android:layout_height="40.0dip"
                              android:layout_centerVertical="true"
                              android:layout_toLeftOf="@+id/chbox_enableWlanMac"
                              android:maxLength="12"
                              android:nextFocusDown="@+id/tedit_wpanMac"/&gt;
  
                      &lt;CheckBox
                              android:id="@+id/chbox_enableWlanMac"
                              android:layout_width="wrap_content"
                              android:layout_height="wrap_content"
                              android:layout_alignParentRight="true"
                              android:layout_centerVertical="true"
                              android:checked="true"/&gt;
  
                  &lt;/RelativeLayout&gt;
  
              &lt;/LinearLayout&gt;
  
              &lt;LinearLayout
                      xmlns:android="http://schemas.android.com/apk/res/android"
                      android:id="@+id/group_wpanMac"
                      android:orientation="vertical"
                      android:layout_width="match_parent"
                      android:layout_height="wrap_content"
                      android:layout_below="@+id/group_wlanMac"
                      android:background="@drawable/bcg_button" &gt;
  
                  &lt;TextView
                          android:id="@+id/tview_wpanMac"
                          android:layout_width="match_parent"
                          android:layout_height="wrap_content"
                          android:layout_marginLeft="25dp"
                          android:layout_marginRight="20dp"
                          android:layout_marginTop="10dp"
                          android:layout_marginBottom="5dp"
                          android:textColor="#ffffff"
                          android:text="@string/txt_wpanMac" /&gt;
  
                  &lt;RelativeLayout
                          android:layout_height="wrap_content"
                          android:layout_width="match_parent"
                          android:layout_marginLeft="20dp"
                          android:layout_marginRight="20dp"
                          android:layout_marginBottom="10dp"&gt;
  
                      &lt;EditText
                              android:id="@+id/tedit_wpanMac"
                              android:layout_width="match_parent"
                              android:layout_height="40dp"
                              android:layout_centerVertical="true"
                              android:layout_toLeftOf="@+id/chbox_enableWpanMac"
                              android:inputType="text"/&gt;
  
                      &lt;CheckBox
                              android:id="@+id/chbox_enableWpanMac"
                              android:layout_width="wrap_content"
                              android:layout_height="wrap_content"
                              android:layout_alignParentRight="true"
                              android:layout_centerVertical="true"
                              android:checked="true"/&gt;
  
                  &lt;/RelativeLayout&gt;
  
              &lt;/LinearLayout&gt;
  
              &lt;!-- SOME OTHER LAYOUTS... --&gt;
  
          &lt;/RelativeLayout&gt;
  
      &lt;/ScrollView&gt;
  
      &lt;android.inputmethodservice.KeyboardView
              style="@style/Keyboard"
              android:id="@+id/keyboard_mac"
              android:layout_height="wrap_content"
              android:layout_width="match_parent"
              android:layout_alignParentBottom="true"
              android:visibility="gone"/&gt;
  
  &lt;/RelativeLayout&gt;
  </code></pre>
  
  <p>activity</p>
  
  <pre><code>public class _identificationChange extends _Activity
  {
  
      EditText mEdit_wlanMac;
      EditText mEdit_wpanMac;
  
      MacKeyboard mKeyboardMac = null;
  
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
  
          setContentView(R.layout.identification_change);
  
          mEdit_wlanMac = (EditText) findViewById(R.id.tedit_wlanMac);
          mEdit_wpanMac = (EditText) findViewById(R.id.tedit_wpanMac);
  
          KeyboardView keyboardView = (KeyboardView) findViewById(R.id.keyboard_mac);
          Keyboard keyboard = new Keyboard(this, R.xml.keyboard_mac);
          keyboardView.setKeyboard(keyboard);
          keyboardView.setEnabled(true);
          keyboardView.setPreviewEnabled(false);
  
          mKeyboardMac = new MacKeyboard(keyboardView);
          keyboardView.setOnKeyboardActionListener(mKeyboardMac);
  
          mEdit_wlanMac.setOnFocusChangeListener(
                  new View.OnFocusChangeListener() {
                      @Override
                      public void onFocusChange(View view, boolean hasFocus) {
                          if ( hasFocus ) {
                              mKeyboardMac.show(mEdit_wlanMac);
                          } else {
                              mKeyboardMac.hide();
                          }
                      }
                  }
          );
  
          mEdit_wlanMac.setOnClickListener(
                  new View.OnClickListener() {
                      @Override
                      public void onClick(View view) {
                          mKeyboardMac.show(mEdit_wlanMac);
                      }
                  }
          );
  
      }
  
      @Override
      public void onBackPressed() {
          if ( mKeyboardMac.isVisible() ) {
              mKeyboardMac.hide();
          } else {
              super.onBackPressed();
          }
      }
  }
  </code></pre>
  
  <p>I will appreciate any help.</p>