Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

requestFocus() returning false

I am inflating a view when a button is pressed. The inflated view is like a dialog box, however when I try to get focus on the dialog box with requestFocus(), requestFocus() returns false means I am not getting focus on the view, but when I manually tap the inflated view, it recieves focus.

What am I doing wrong?

I clear focus from the button (which is used to inflated the new view) before I call requestFocus on inflated view.

Regards

like image 566
Naruto Avatar asked Dec 19 '22 19:12

Naruto


1 Answers

A possible reason why the View.requestFocus() method returns false (does not successfully request focus) is because of the following reason:

"A view will not actually take focus if it is not focusable (isFocusable() returns false), or if it is focusable and it is not focusable in touch mode (isFocusableInTouchMode()) while the device is in touch mode."

Check if both isFocusable() and isFocusableInTouchMode() return true. If not, you could possibly force them by using the setFocusable() and setFocusableInTouchMethod() as seen below.

View view;
view.setFocusable(true);
view.setFocusableInTouchMode(true);
view.requestFocus();
like image 116
dsapalo Avatar answered Dec 27 '22 12:12

dsapalo