How to show soft-keyboard when edittext is focused
To force the soft keyboard to appear, you can use
EditText yourEditText= (EditText) findViewById(R.id.yourEditText);
yourEditText.requestFocus();
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(yourEditText, InputMethodManager.SHOW_IMPLICIT);
And for removing the focus on EditText
, sadly you need to have a dummy View
to grab focus.
To close it you can use
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(yourEditText.getWindowToken(), 0);
This works for using it in a dialog
public void showKeyboard(){
InputMethodManager inputMethodManager = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
}
public void closeKeyboard(){
InputMethodManager inputMethodManager = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0);
}
How can I set the focus (and display the keyboard) on my EditText programmatically
Try this:
EditText editText = (EditText) findViewById(R.id.myTextViewId);
editText.requestFocus();
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT);
http://developer.android.com/reference/android/view/View.html#requestFocus()
Android: force keyboard to appear and focus on EditText
Thanks to @Shubham's link I was able to figure it out. The solution was not the answer given in the link, however. It was the second answer.
editText.requestFocus();
InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY);
Edit:
once using the above solution the keyboard will remain on the screen until a user presses either the back button or the home button (sometimes it takes a few times). To remove the keyboard use this
imm.toggleSoftInputFromWindow(rootView.getWindowToken(), 0,0);
in my case rootView is the rootView of the current activity. I have not tested this to see if this will work on child views.
Android: show soft keyboard automatically when focus is on an EditText
You can create a focus listener on the EditText
on the AlertDialog
, then get the AlertDialog
's Window
. From there you can make the soft keyboard show by calling setSoftInputMode
.
final AlertDialog dialog = ...;
editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
}
}
});
force soft keyboard to show when EditText gets focus
This is how I show the ketyboard:
EditText yourEditText= (EditText) findViewById(R.id.yourEditText);
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(yourEditText, InputMethodManager.SHOW_IMPLICIT);
How Show soft keyboard automatically when EditText receives focus
You can add flags to your activity as well which will show the keyboard automatically
this is mostly useful if you expect the focus to be applied when the activity launches
Also you can use in Fragment:
InputMethodManager imm = (InputMethodManager)getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY);
or in Activity
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY);
Soft Keyboard shows up on EditText focus ONLY once
Try to open and hide inside a Runnable
as,
TO OPEN
ettext.requestFocus();
ettext.postDelayed(new Runnable() {
@Override
public void run() {
InputMethodManager keyboard = (InputMethodManager)
getSystemService(Context.INPUT_METHOD_SERVICE);
keyboard.showSoftInput(ettext, 0);
}
},200);
TO CLOSE
ettext.requestFocus();
ettext.postDelayed(new Runnable() {
@Override
public void run() {
InputMethodManager keyboard = (InputMethodManager)
getSystemService(Context.INPUT_METHOD_SERVICE);
keyboard.hideSoftInputFromWindow(ettext.
getWindowToken(), 0);
}
},200);
How to stop Soft keyboard showing automatically when focus is changed (OnStart event)
Maybe this will help you:
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
Put this method in the onCreate()
method of your activity
when the activity is first created or called.
Related Topics
Handler VS Asynctask VS Thread
How to Go Back to Previous Page If Back Button Is Pressed in Webview
How to See If Wi-Fi Is Connected on Android
How to Get the External Sd Card Path For Android 4.0+
Instant Run in Android Studio 2.0 (How to Turn Off)
How to Start an Application Using Android Adb Tools
How to Update Gradle in Android Studio
Recyclerview Inside Scrollview Is Not Working
How to Define Callbacks in Android
How to Create Our Own Listener Interface in Android
Programmatically Register a Broadcast Receiver
Constraintlayout Views in Top Left Corner
How to Prevent a Dialog from Closing When a Button Is Clicked
How to Exit from the Application and Show the Home Screen
Fling Gesture Detection on Grid Layout
How to Get the Android Device'S Primary E-Mail Address
How to Manually Include External Aar Package Using Gradle For Android