Skip to content Skip to sidebar Skip to footer

How To Hide Default Android Keyboard When Custom Keyboard Is Visible?

I have an Android app with Activity containing a TextView as below. Copy

Solution 2:

use this method for instant hide keyboard

voidhideSoftKeyboard(Activity activity) {
    InputMethodManagerinputMethodManager= (InputMethodManager)  activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
    inputMethodManager.hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(), 0);
}

Solution 3:

After a lot of trial and error, I found out how to do this.

Earlier, I had placded the keyboard-hiding code in the onTouch(...) handler of the EditText. For whatever reason, that does not work. It still shows both keyboards (default and custom).

I moved that code to the activity as follows:

privateOnTouchListener m_onTouchListenerEditText = newOnTouchListener()
{
    @OverridepublicbooleanonTouch(View v, MotionEvent event)
    {
        if (v != m_myEditText)
        {
            // This is the function which hides the custom keyboard by hiding the keyboard view in the custom keyboard class (download link for this class is in the original question)
            m_customKeyboard.hideCustomKeyboard();
            ((EditText) v).onTouchEvent(event);
        }

        returntrue;
    }
};

privatevoidhideDefaultKeyboard()
{
    getWindow().setSoftInputMode(
            WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
}

Solution 4:

I had the same problem. The solution is put setInputType after the onTouchEvent.

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 typefloat x = event.getX();
float y = event.getY();
int touchPosition = edittext.getOffsetForPosition(x, y);
if (touchPosition  > 0){
    edittext.setSelection(touchPosition);
}
returntrue; // Consume touch event

Solution 5:

These codes work perfectly from the LOLLIPOP (v21) or later:

edittext.setOnTouchListener(newView.OnTouchListener() {
    @OverridepublicbooleanonTouch(View v, MotionEvent event) {
        EditTexteditText= (EditText) v;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            editText.setShowSoftInputOnFocus(false); // disable android keyboardreturnfalse;
        } else {
            // IMPORTANT NOTE : EditText touching disorderintinType= editText.getInputType(); // backup the input type
            editText.setInputType(InputType.TYPE_NULL); // disable soft input
            editText.onTouchEvent(event); // call native handler
            editText.setInputType(inType); // restore input typereturntrue; // Consume touch event
        }
    }
});

Post a Comment for "How To Hide Default Android Keyboard When Custom Keyboard Is Visible?"