1

I have an Activity with only one EdtiText. When that Activity starts, the EditText is focused and the soft keyboard is shown. This seem to happen after onResume, because when I programmatically hide the keyboard in onResume it doesn't work. When I do this:

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

    new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
            InputMethodManager imm = (InputMethodManager) getSystemService(Activity.INPUT_METHOD_SERVICE);
            //Find the currently focused view, so we can grab the correct window token from it.
            //If no view currently has focus, create a new one, just so we can grab a window token from it
            imm.hideSoftInputFromWindow(etBarcode.getWindowToken(), 0);
        }
    }, 500);
}

it hides it (after popping up shortly).

Is there an event on an EditText I can use to preven the keyboard popping up? Or some other way of preventing it to show?

Update focusableInTouchMode does not do what I want, because when set to true the keyboard pops up, when set to false it is not focusable at all.

Bart Friederichs
  • 33,050
  • 15
  • 95
  • 195
  • 1
    If these question can help https://stackoverflow.com/questions/1555109/stop-edittext-from-gaining-focus-at-activity-startup – iMDroid Sep 29 '17 at 11:51

4 Answers4

3
// Add following code in activity onCreate
        this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
jessica
  • 1,700
  • 1
  • 11
  • 17
0

For parent layout android:focusableInTouchMode="true"

Romadro
  • 626
  • 4
  • 10
  • This doesn't work, see my update. Set to `true`, the keyboard still pops up, when `false` it is not focusable. – Bart Friederichs Sep 29 '17 at 11:58
  • That's strange. Maybe in manifest you have not default realization of windowSoftInputMode or maybe you have requestFocus inside your layout xml – Romadro Sep 29 '17 at 12:04
0

You can set property of Layout like

android:descendantFocusability="beforeDescendants"
android:focusableInTouchMode="true"
Muhammad Tufail
  • 326
  • 5
  • 14
0

The problem is very complicated, as it's about views getting the focus, and how it's handled by all the layout, about the touch mode being focusable, and last but not least about how the soft keyboard handles that. But this works for me:

In manifest:

android:windowSoftInputMode="stateHidden|stateAlwaysHidden"

In layout:

android:focusable="true"
android:focusableInTouchMode="true"

and last but not least, touch listener set to the EditText to prevent the soft keyboard to show up once touched:

        mMyText.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            // forward the touch event to the view (for instance edit text will update the cursor to the touched position), then
            // prevent the soft keyboard from popping up and consume the event
            v.onTouchEvent(event);
            disableSoftKeyboard(MyActivity.this);
            return true;
        }
    });

whereas the method does more or less what you're doing already:

public void disableSoftKeyboard(@NonNull Activity activity) {
    View view = activity.getCurrentFocus();
    if (view != null) {
        InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
    } else {
        activity.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
    }
}

Hope it helps, and also that I didn't forget anything :)

Alessio
  • 3,063
  • 1
  • 15
  • 17