Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hide soft keyboard on pressing back

I have an EditText in an Activity and I want it to be active and soft-keyboard be open when I open that Activity. Here is my xml for EditText:

<EditText
    android:background="@null"
    android:cursorVisible="true"
    android:elegantTextHeight="true"
    android:enabled="true"
    android:focusable="true"
    android:hint="Search"
    android:id="@+id/editText11"
    android:inputType="textNoSuggestions|textCapSentences"
    android:layout_centerVertical="true"
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    android:singleLine="true"
    android:textColor="#000000"
    android:textCursorDrawable="@null" />

and I have used android:windowSoftInputMode="stateVisible" for the activity in which I have this EditText.

The problem is, when I press back once, the keyboard does not hide(ideally it does in all other EditTexts) and when I press back again, it closes the Activity. On the first back press, I am not getting a call to onBackPressed() while on the second back press, I do. Why is this kind of behaviour is happening and how to resolve it?

Edit What I want is, if keyboard is open, pressing back should close the keyboard and if the keyboard is not open, then close the activity.

like image 516
Amit Tiwari Avatar asked May 09 '16 10:05

Amit Tiwari


People also ask

How do I hide my soft keyboard?

Hiding the Soft Keyboard Programmatically You can force Android to hide the virtual keyboard using the InputMethodManager, calling hideSoftInputFromWindow, passing in the token of the window containing your edit field. This will force the keyboard to be hidden in all situations.

How do I prevent the soft keyboard from pushing my view up in fragment?

Setting android:isScrollContainer = "false" inside the ScrollView worked for me. According to the documentation, settings "isScrollContainer" to true means that the scroll view can be resized to shrink its overall window so that there is space for an input method.


3 Answers

I add below code in my BaseActivity.java

@Override
protected void onPause() {
    super.onPause();

    final InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Activity.INPUT_METHOD_SERVICE);
    if (inputMethodManager != null && inputMethodManager.isActive()) {
        if (getCurrentFocus() != null) {
            inputMethodManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
        }
    }
}
like image 95
Better Avatar answered Oct 23 '22 15:10

Better


Try this ...

create class called Util and put this code

public static void hideSoftKeyboard(Activity activity) {
    final InputMethodManager inputMethodManager = (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
    if (inputMethodManager.isActive()) {
        if (activity.getCurrentFocus() != null) {
            inputMethodManager.hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(), 0);
        }
    }
}

and call on the onBackPressed() of Activity

like image 25
Nrup Parikh Avatar answered Oct 23 '22 17:10

Nrup Parikh


Try like this,

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (event.getAction() == KeyEvent.ACTION_DOWN) {
        switch (keyCode) {
            case KeyEvent.KEYCODE_BACK:
                 //useful for hiding the soft-keyboard is:
                 getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);

                return true;
        }

    }
    return super.onKeyDown(keyCode, event);
}

This may helps you

like image 35
Sathish Kumar J Avatar answered Oct 23 '22 17:10

Sathish Kumar J