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?
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:
setFocusableInTouchMode(true)
will ensure that the FOCUSABLE
flag is set.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.
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