I am developing an Android application in Java in Android Studio.The environment is as follows
Environment minSdkVersion: 29 targetSdkVersion: 35 Device: Android device with physical keys Use of custom EditTextStyle: Yes
The process of screen transition is implemented by clicking a button. I want to prevent the software keyboard from being displayed when EditText has the focus after the screen transition. setShowSoftInputOnFocus(false) is set, but the keyboard is displayed under the following conditions
The software keyboard is displayed only at the first input immediately after the Enter key screen transition on the physical keyboard. (The software keyboard is not displayed when the screen is transitioned by tapping the button on the screen.) After typing once, it hides normally. What we are looking for
How to make sure that the software keyboard is not displayed when the screen transitions and the first time input is made, even if EditText has the focus. In particular, a way to achieve the same behavior when the screen transitions with the Enter key on the physical keyboard. If possible, a systematic solution rather than hard-coding
The contents are converted from Japanese to English at DeepL.
I am developing an Android application in Java in Android Studio.The environment is as follows
Environment minSdkVersion: 29 targetSdkVersion: 35 Device: Android device with physical keys Use of custom EditTextStyle: Yes
The process of screen transition is implemented by clicking a button. I want to prevent the software keyboard from being displayed when EditText has the focus after the screen transition. setShowSoftInputOnFocus(false) is set, but the keyboard is displayed under the following conditions
The software keyboard is displayed only at the first input immediately after the Enter key screen transition on the physical keyboard. (The software keyboard is not displayed when the screen is transitioned by tapping the button on the screen.) After typing once, it hides normally. What we are looking for
How to make sure that the software keyboard is not displayed when the screen transitions and the first time input is made, even if EditText has the focus. In particular, a way to achieve the same behavior when the screen transitions with the Enter key on the physical keyboard. If possible, a systematic solution rather than hard-coding
The contents are converted from Japanese to English at DeepL.
Share asked Apr 1 at 3:30 らむねらむね 1 New contributor らむね is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct. 2- stackoverflow/questions/1109022/… – vbijoor Commented Apr 1 at 4:59
- This question is similar to: How can I close/hide the Android soft keyboard programmatically?. If you believe it’s different, please edit the question, make it clear how it’s different and/or how the answers on that question are not helpful for your problem. – TechyBenji Commented Apr 1 at 6:57
1 Answer
Reset to default 1Step 1: In your EditText
, disable soft keyboard on focus
editText.setShowSoftInputOnFocus(false);
Step 2: In the AndroidManifest.xml
, hide the keyboard when the activity starts
Set this flag for the activity you're transitioning to:
<activity
android:name=".TargetActivity"
android:windowSoftInputMode="stateHidden|adjustResize" />
Step 3: (Optional but robust) Force-hide the keyboard in onResume()
of the target activity
Just in case any edge case slips through (e.g. from physical Enter key), add this:
@Override
protected void onResume() {
super.onResume();
View view = getCurrentFocus();
if (view != null) {
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
}
Alternative solution is like : Delay Focus Handling to Avoid Initial Keyboard Trigger
If keyboard still appears due to system race conditions, delay focus handling like this:
editText.setShowSoftInputOnFocus(false);
editText.clearFocus(); // ensure it's not focused prematurely
new Handler(Looper.getMainLooper()).postDelayed(() -> {
editText.requestFocus();
}, 100); // slight delay to bypass the default system behavior