1

Situation

I have an Android app that uses an external keyboard and want to hide the soft keyboard when an Entry control gets focus.

Reference

Following this android documentation it states the following:

Note: If the user's device has an attached hardware keyboard, the soft input method does not appear.

However, in some Android devices, when the Entry gets focus the soft input does appear.

How can I get to hide in those cases the soft input?

Thanks in advance!

Ignacio Gómez
  • 1,587
  • 4
  • 23
  • 41

2 Answers2

1

You can hide the softkeyboard with this code

  getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);

Get an instance of your EditText

EditText editText = (EditText) findViewById(R.id.edittext);

To prevent the Default SoftKeyboard from appear in the EditText, override the following events

// Make the custom keyboard appear

    edittext.setOnFocusChangeListener(new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            if (hasFocus) showCustomKeyboard(v);
            else hideCustomKeyboard();
        }
    });
    edittext.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            showCustomKeyboard(v);
        }
    });
    edittext.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            EditText edittext = (EditText) v;
            int inType = edittext.getInputType();       // Backup the input type
            edittext.setInputType(InputType.TYPE_NULL); // Disable standard keyboard
            edittext.onTouchEvent(event);               // Call native handler
            edittext.setInputType(inType);              // Restore input type
            return true; // Consume touch event
        }
    });
}
public void hideCustomKeyboard() {
    keyboardView.setVisibility(View.GONE);
    keyboardView.setEnabled(false);
}
public void showCustomKeyboard( View v) {
    keyboardView.setVisibility(View.VISIBLE);
    keyboardView.setEnabled(true);
    if( v!=null ){
        ((InputMethodManager)getSystemService(Activity.INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(v.getWindowToken(), 0);
    }
}
public boolean isCustomKeyboardVisible() {
    return keyboardView.getVisibility() == View.VISIBLE;
}
@Override public void onBackPressed() {
    if( isCustomKeyboardVisible() ) hideCustomKeyboard(); else this.finish();
}

Disclaimer:- I have implemented custom keyboard in my app and I wrote this tutorial - http://inducesmile.com/android/how-to-create-an-android-custom-keyboard-application/

Inducesmile
  • 2,475
  • 1
  • 17
  • 19
0

This works for me :

public static void DisableSoftKeyboard (EditText editText)
{
    ((InputMethodManager)GetSystemService(Context.InputMethodService)).HideSoftInputFromWindow(editText.WindowToken, 0);
    if (Build.VERSION.SdkInt >= BuildVersionCodes.Honeycomb) {
        editText.SetRawInputType (InputTypes.ClassText);
        editText.SetTextIsSelectable (true);
    } else {
        editText.SetRawInputType (InputTypes.Null);
        editText.Focusable = true;
    }
}

Implementation :

editText.FocusChange+= (sender, e) => {
    DisableSoftKeyboard(editText);
};
Yksh
  • 3,276
  • 10
  • 56
  • 101