When my fragment starts, I want my edittext to be in focus/let user to just start typing in it. I am able to get it in focus with requestFocus(), but I cannot get the keyboard to show up.
I have tried both this:
edit = (EditText) view.findViewById(R.id.search);
edit.requestFocus();
InputMethodManager imgr = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
imgr.showSoftInput(edit, 0);
and
edit = (EditText) view.findViewById(R.id.search);
InputMethodManager imgr = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
imgr.showSoftInput(edit, 0);
edit.requestFocus();
How can I get the keyboard to show up for EditText?
Another way to make the keyboard open when your fragment starts is to call
requestFocus()
inonCreateView
and react accordingly by opening the keyboard if and only if theEditText
is focusable.this article helped me
https://turbomanage.wordpress.com/2012/05/02/show-soft-keyboard-automatically-when-edittext-receives-focus/
Does this work?
I have helpful extension for that:
You will also need this one:
After trying all solutions here and on other questions related, Here's the method that works for me:
The fun fact is when you call it without postDeleayed it won't work, even if you just delay it for 1 millisecond it still won't work :D
You can also use it as an extension like this:
if you don't like to pass activity as a parameter, you use this extension function as @Rafols suggests:
then your method will look like this:
also if you have a text already in your EditText, its better set selection at the end of your existing text:
and if you want to start it when fragment starts: