Answer the question
In order to leave comments, you need to log in
How to disable the automatic display of the keyboard when launching an activity with an EditText?
Situation:
There is a LinearLayout with EditText. When the activity is launched, the focus is set to this input field, but the keyboard is not revealed. If you add a ListView under the EditText, at startup the keyboard is displayed and closes it, which is highly undesirable.
If you close the keyboard in portrait orientation, it reopens when you switch to landscape.
If you close it in landscape orientation, it does not appear when you switch to portrait (layout.xml is one).
<requestFocus /> removed.
Creating a nested invisible layout that captures focus before the input field is not an option, I need the focus to be on the EditText.
Currently in use
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
in onCreate(), but it looks like a wild crutch. Answer the question
In order to leave comments, you need to log in
Add to AndroidManifest.xml, for desired activity: android:windowSoftInputMode="stateHidden"
final EditText editText = (EditText) findViewById(R.id.edit_text);
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(editText.getWindowToken(), 0);
I also puzzled over this problem for a long time. I went through many ways, but they all had compatibility issues with platform versions. As a result, I settled on the onTouchListener handler for EditText:
editText.setOnTouchListener(onTouchListener);
View.OnTouchListener onTouchListener = new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
editTextPinCode.setFocusable(true);
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
Layout layout = ((EditText) v).getLayout();
float x = event.getX() + editTextPinCode.getScrollX();
int offset = layout.getOffsetForHorizontal(0, x);
if(offset>0)
if(x>layout.getLineMax(0))
editTextPinCode.setSelection(offset);
else
editTextPinCode.setSelection(offset - 1);
break;
}
return true;
}
};
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question