Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why are there 2 setFocusable methods in Android?

I was trying to set the focusability of a component and found these two methods, hoping I could use them to make the component only focusable when the user touches it, not when requested programmatically:

myComponent.setFocusable(false);
myComponent.setFocusableInTouchMode(true);

Then I looked at their documentation:

public void setFocusable (boolean focusable)

Set whether this view can receive the focus. Setting this to false will also ensure that this view is not focusable in touch mode.


public void setFocusableInTouchMode (boolean focusableInTouchMode)

Set whether this view can receive focus while in touch mode. Setting this to true will also ensure that this view is focusable.

So, if calling either one implicitly calls the other, why make the distinction?

like image 328
Ky. Avatar asked Dec 25 '22 22:12

Ky.


1 Answers

There are two flags in the view system: FOCUSABLE and FOCUSABLE_IN_TOUCH_MODE. Each method sets/clears its respective flag, and there are two cases where one method affects the state of the other:

  1. Calling setFocusableInTouchMode(true) will ensure that the FOCUSABLE flag is set.
  2. Calling setFocusable(false) will ensure that the FOCUSABLE_IN_TOUCH_MODE flag is cleared.

In other words, FOCUSABLE_IN_TOUCH_MODE depends on the global FOCUSABLE flag, and cannot be set without it.

The distinction in mode is a bit legacy. The idea was to distinguish between when the user was navigating the UI with a D-pad or trackball versus tapping on a touch screen. These days, devices are pretty much always in "touch mode", but there are still a few cases where the underlying FOCUSABLE flag is checked. Mainly when hunting for the "next" view from an input method's return key or when accessibility is turned on.

For more details on the different modes, you can read the "Focus Handling" and "Touch Mode" sections of the SDK View Documentation.

like image 93
devunwired Avatar answered Jan 05 '23 17:01

devunwired