I have a layout which contains some views like this:
&
First way:
etPassword.post(() -> {
etPassword.requestFocus();
InputMethodManager manager = (InputMethodManager) getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
manager.showSoftInput(etPassword, InputMethodManager.SHOW_IMPLICIT);
});
Second way:
In Manifest:
<activity
android:name=".activities.LoginActivity"
android:screenOrientation="portrait"
android:windowSoftInputMode="stateVisible"/>
In code:
etPassword.requestFocus();
This worked for me, Thanks to ungalcrys
Show keyboard:
editText = (EditText)findViewById(R.id.myTextViewId);
editText.requestFocus();
InputMethodManager imm = (InputMethodManager)getSystemService(this.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED,InputMethodManager.HIDE_IMPLICIT_ONLY);
Hide keyboard:
InputMethodManager imm = (InputMethodManager) getSystemService(this.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(editText.getWindowToken(), 0);
Here is how a kotlin extension for showing and hiding the soft keyboard can be made:
fun View.showKeyboard() {
this.requestFocus()
val inputMethodManager = context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
inputMethodManager.showSoftInput(this, InputMethodManager.SHOW_IMPLICIT)
}
fun View.hideKeyboard() {
val inputMethodManager = context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
inputMethodManager.hideSoftInputFromWindow(windowToken, 0)
}
Then you can just do this:
editText.showKeyboard()
// OR
editText.hideKeyboard()
I tried the top answer by David Merriman and it also didn't work in my case. But I found the suggestion to run this code delayed here and it works like a charm.
val editText = view.findViewById<View>(R.id.settings_input_text)
editText.postDelayed({
editText.requestFocus()
val imm = context.getSystemService(INPUT_METHOD_SERVICE) as? InputMethodManager
imm?.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT)
}, 100)
I tried a lot ways and it's not working tho, not sure is it because i'm using shared transition from fragment to activity containing the edit text.
Btw my edittext is also wrapped in LinearLayout.
I added a slight delay to request focus and below code worked for me: (Kotlin)
et_search.postDelayed({
editText.requestFocus()
showKeyboard()
},400) //only 400 is working fine, even 300 / 350, the cursor is not showing
showKeyboard()
val imm = getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0)
I couldn't get any of these answers to work on their own. The solution for me was to combine them:
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY);
editText.requestFocus();
imm.showSoftInput(editText, InputMethodManager.SHOW_FORCED);
I'm not sure why that was required for me -- according to the docs it seems that either method should have worked on their own.