14

For some EditText views I want to use a custom keyboard instead of the soft one.

The problem is when I click on an EditText for the first time both keyboards are shown. When I click for the second time - the soft keyboard finally disappeares.

What might be the reason for such behavior?

Here's the code I use:

package pkleczek.profiwan.keyboards;

import android.app.Activity;
import android.inputmethodservice.KeyboardView;
import android.text.InputType;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;

public abstract class CustomKeyboard {

    /** A link to the KeyboardView that is used to render this CustomKeyboard. */
    protected KeyboardView mKeyboardView;
    /** A link to the activity that hosts the {@link #mKeyboardView}. */
    protected Activity mHostActivity;

    /** Returns whether the CustomKeyboard is visible. */
    public boolean isCustomKeyboardVisible() {
        return mKeyboardView.getVisibility() == View.VISIBLE;
    }

    /**
     * Make the CustomKeyboard visible, and hide the system keyboard for view v.
     */
    public void showCustomKeyboard(View v) {
        mKeyboardView.setVisibility(View.VISIBLE);
        mKeyboardView.setEnabled(true);

        if (v != null) {
            InputMethodManager inputManager = (InputMethodManager) mHostActivity
                    .getSystemService(Activity.INPUT_METHOD_SERVICE);
            inputManager.hideSoftInputFromWindow(v.getWindowToken(), 0);
        }
    }

    /** Make the CustomKeyboard invisible. */
    public void hideCustomKeyboard() {
        mKeyboardView.setVisibility(View.GONE);
        mKeyboardView.setEnabled(false);
    }

    /**
     * Register <var>EditText<var> with resource id <var>resid</var> (on the
     * hosting activity) for using this custom keyboard.
     * 
     * @param resid
     *            The resource id of the EditText that registers to the custom
     *            keyboard.
     */
    public void registerEditText(int resid) {
        EditText edittext = (EditText) mHostActivity.findViewById(resid);

        edittext.setOnFocusChangeListener(new OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {
                    showCustomKeyboard(v);
                } else {
                    hideCustomKeyboard();
                }
            }
        });

        edittext.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                showCustomKeyboard(v);
            }
        });

        edittext.setInputType(edittext.getInputType()
                | InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS);
    }
}
Paweł Kłeczek
  • 543
  • 1
  • 4
  • 25

2 Answers2

10

Try to put the following condition in your InputMethodManager code before inputManager.hideSoftInputFromWindow(v.getWindowToken(), 0);line:

    if (inputManager!=null) {
                Activity activity = getActivity();
                if (acvivity == null)
                    return;
                if (activity.getCurrentFocus() == null)
                    return;
                if (activity.getCurrentFocus().getWindowToken() == null)
                    return;
                inputManager.hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(), 0);
            }

I used it in ListFragment to hide the default keyboard. First when i press an EditText and keyboard is shown and when on onScrollStateChanged i hide it.

You can also try to put the InputMethodManager code inside the onClickListener of the EditText and then call your showCustomKeyboard() method.

Put an Else statement and Log in it after the if (v != null), maybe your View v is null.

taxo
  • 470
  • 3
  • 17
  • Unfortunately, the problem persists. I've checked it with debugger and all `hideSoftInputFromWindow()` methods were called. – Paweł Kłeczek Feb 05 '14 at 11:43
  • Try to use `setOnTouchListener()` instead of `setOnClickListener()` for your **EditText**. Put **Log** in your methods, this will show you best if they are actually called. – taxo Feb 05 '14 at 13:29
  • 1
    Huh.. No progress. Log shows that all methods are called, but still the soft keyboard disappeares only after I click `EditText` for the second time... – Paweł Kłeczek Feb 05 '14 at 15:16
  • Hi, `activity` can never be `null` :) – Bokili Production Apr 11 '21 at 16:31
1

The solution provided by Rotemmiz in the topic Close/hide the Android Soft Keyboard worked for me.

An abstract:

public void setEditTextFocus(EditText editText, boolean isFocused)
{
    editText.setCursorVisible(isFocused);
    editText.setFocusable(isFocused);
    editText.setFocusableInTouchMode(isFocused);

    if (isFocused)
    {
        editText.requestFocus();
    }
}
Community
  • 1
  • 1
Paweł Kłeczek
  • 543
  • 1
  • 4
  • 25