Search code examples
androidandroid-fragmentsandroid-softkeyboardshow-hide

How to disable the soft keyboard to show up when the user clicks on EditText?


I created a custom keyboard using CardView, so I don't need the software keyboard when I have an EditText. The thing now is that when I click on EditText the virtual keyboard pops up, is there a way to disable the virtual keyboard?

Here is my fragment code:

public class FragmentQuestion1 extends Fragment {

    private EditText mEditTextQuestion1;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_question_1, container, false);

        mEditTextQuestion1 = view.findViewById(R.id.edit_text_question_1);
        MyKeyboard keyboard = view.findViewById(R.id.keyboard);
        mEditTextQuestion1.setRawInputType(InputType.TYPE_CLASS_TEXT);
        mEditTextQuestion1.setTextIsSelectable(true);

        InputConnection ic = mEditTextQuestion1.onCreateInputConnection(new EditorInfo());
        keyboard.setInputConnection(ic);

        mEditTextQuestion1.addTextChangedListener(new NumberTextWatcher(mEditTextQuestion1));

mEditTextQuestion1.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View view, boolean hasFocus) {
                if (hasFocus) {
                    getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
                } else {

                }
            }
        });

        return view;
    }

Here is the way I used the EditText in my xml file:

<EditText
            android:id="@+id/edit_text_question_1"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginLeft="8dp"
            android:layout_marginRight="8dp"
            android:backgroundTint="#FFFFFF"
            android:inputType="number"
            android:gravity="center"
            android:focusable="true"
            android:focusableInTouchMode="true"
            android:textIsSelectable="true"/>

Solution

  • I found a solution that works fine and is pretty easy:

    Just use mEditTextQuestion1.setEnabled(false); in the inCreateView method to make the EditText not clickable. In my case then the color of the EditText changed to a brighter dark so I added android:textColor="#000"to my xml file.