When MyFgment
appear in screen, I want to set cursor focus on EditText
in MyFragment
automatically. I've tried EditText.requestFocus()
, but it doesn't focus on EditText
. How can I do??
You can use View. OnFocusChangeListener to detect if any view (edittext) gained or lost focus. This goes in your activity or fragment or wherever you have the EditTexts.
You can use EditText. setText(...) to set the current text of an EditText field.
editText.requestFocus() will put focus to your View
if it is focusable . But I guess you want to show keyboard when it is focused. If I am right then the following code might work for you.
editText.requestFocus();
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY);
The code is from this post. You can also check Android Developers for details.
set this in your xml
android:focusable="true"
android:focusableInTouchMode="true"
and you can set this on onViewCreated program editText.isFocusableInTouchMode();
editText.setFocusable(true);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With