11

I have three edit-text fields. Of these fields I want to display the soft input keyboard only for the first field and disabled for the later two fields sice these are date and time fields.

Edit-Text 1 //Show the keyboard
Edit-Text 2 and 3 //Hide the keyboard

By using below code I'm able to disable the keyboard for field 2 and 3 but when the user has focus on field 1 the keyboard appears but didn't hide when user taps on field 2 or 3. Although when field 2 or 3 is tapped first no keyboard appears.

//Code to disable soft input keyboard
public static void disableSoftInputFromAppearing(EditText editText) {
    if (Build.VERSION.SDK_INT >= 11) {
        editText.setRawInputType(InputType.TYPE_CLASS_TEXT);
        editText.setTextIsSelectable(true);
    } else {
        editText.setRawInputType(InputType.TYPE_NULL);
        editText.setFocusable(true);
    }

How can I hide the soft input keyboard if its already open?

Paras
  • 3,191
  • 6
  • 41
  • 77

4 Answers4

22

//activity

public static void hideSoftKeyboard(Activity activity) {

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

// fragment

public void hideSoftKeyboard() {
    InputMethodManager inputMethodManager = (InputMethodManager) getActivity().getSystemService(Activity.INPUT_METHOD_SERVICE);
    inputMethodManager.hideSoftInputFromWindow(getActivity().getCurrentFocus().getWindowToken(), 0);
}

// If edit-text loses the focus, hiding keyboard

edTxtMessage.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if (isEditable){
                v.setFocusable(true);
                v.setFocusableInTouchMode(true);
            } else {
                edTxtMessage.setFocusable(false);
            }

            return false;
        }
    });

edTxtMessage.setOnFocusChangeListener(new View.OnFocusChangeListener({
        @Override
        public void onFocusChange(View view, boolean b) {
            if (!b){
                hideKeyboard(getContext(), view);
            }
        }
    });

private void hideKeyboard(Context context, View view) {
    if (view != null) {
        InputMethodManager imm = (InputMethodManager)context.getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
    }
}
P Yellappa
  • 436
  • 5
  • 11
0

Easy way is using the xml

android:inputType="none"
android:textIsSelectable="true
johnrao07
  • 6,690
  • 4
  • 32
  • 55
  • I've already tried that. If I tap on the first field the keyboard appears and it didn't go away until I press the down button – Paras Jul 16 '16 at 11:37
0

You can set the two attributes to your edittext

  android:focusable="false"
  android:focusableInTouchMode="false"

Note: By this Your edittext is clickable for the date and time not focus to softkeyboard

Arjun saini
  • 4,223
  • 3
  • 23
  • 51
0

Check if the current focus is not null, otherwise it might lead to a null pointer exception

if (getCurrentFocus() != null) {
        InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Activity.INPUT_METHOD_SERVICE);
        inputMethodManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
}
Anubhav
  • 1,984
  • 22
  • 17