Hide soft keyboard when pressed

I have EditTextin Activity, and I want it to be active, and the soft keyboard is open when I open it Activity. Here is my xmlfor 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 used android:windowSoftInputMode="stateVisible"for the activity in which I have this one EditText.

The problem is that when I press backonce, the keyboard does not hide (ideally this applies to all other EditTexts), and when I press again back, it closes Activity. The first press, backI do not receive the call onBackPressed(), and the second backpress. Why is this behavior happening and how to solve it?

. , , , , .

+4
5

...

Util

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);
        }
    }
}

onBackPressed() Activity

+8

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);
        }
    }
}
+1

public static void hideKeyBoardMethod(final Context con, final View view) {
    try {
        view.post(new Runnable() {
            @Override
            public void run() {
                InputMethodManager imm = (InputMethodManager) con.getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
            }
        });

    } catch (Exception e) {
        e.printStackTrace();
    }
}
0

,

@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);
}

0

:

, , ,

I tested my code, it works fine.

Step 1: Create CustomEditTextas shown below.

 <com.yourpackage.yourappname.CustomEditText
        android:id="@+id/edittext"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

Step 2: Create a Class CustomEditText.java.

public class CustomEditText extends EditText {

    Context context;
    private static Activity mSearchActivity;;

    public CustomEditText(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
    }

    @Override
    public boolean dispatchKeyEventPreIme(KeyEvent event) {
        if(mSearchActivity != null && event.getKeyCode() == KeyEvent.KEYCODE_BACK){
            KeyEvent.DispatcherState state = getKeyDispatcherState();

            if(state != null){
                if(event.getAction() == KeyEvent.ACTION_DOWN && event.getRepeatCount() == 0){

                    InputMethodManager mgr = (InputMethodManager)
                            context.getSystemService(Context.INPUT_METHOD_SERVICE);
                    mgr.hideSoftInputFromWindow(this.getWindowToken(), 0);
                }
                else if(event.getAction() == KeyEvent.ACTION_UP && !event.isCanceled() && state.isTracking(event)){
                    mSearchActivity.onBackPressed();
                    return true;
                }
            }
        }


        return super.dispatchKeyEventPreIme(event);
    }    

}

Step 3: In your activity, configure CustomEditText and hide the KeyBoard, as shown below.

CustomEditText editText = (CustomEditText) findViewById(R.id.edittext);

        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT);

Step 4: In your activity, just a Overridemethod onBackPressed().

 @Override
    public void onBackPressed() {
        super.onBackPressed();

    }
-1
source

All Articles