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??
Asked
Active
Viewed 4,571 times
7
hanjiman
- 315
- 3
- 13
-
Use Edittext edittext = view.findViewById(R.id.your_id); then if(!edittext.getfocus()) edittext.requestFocus()/ edittext.setFocus(true); – Ranjeet Chouhan Mar 13 '20 at 05:53
3 Answers
12
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.
Naung9
- 201
- 1
- 8
-
Neither requestFocus() not setFocusable working for me at all. – Kalpana Aneyrao Apr 09 '21 at 07:31
5
set this in your xml
android:focusable="true"
android:focusableInTouchMode="true"
and you can set this on onViewCreated program editText.isFocusableInTouchMode();
editText.setFocusable(true);
Amit pandey
- 931
- 1
- 3
- 14
1
Add these lines from class,
EditText.isFocusableInTouchMode();
EditText.setFocusable(true);
EditText.requestFocus();
or add these attributes in layout,
android:focusable="true"
android:focusableInTouchMode="true"
Rashiq
- 650
- 1
- 7
- 22