[ACCEPTED]-What is the difference between setFocusable and setFocusableInTouchMode?-focus

Accepted answer
Score: 31

setFocusable mainly used for enable/disable view's focus 5 event on both touch mode and keypad mode( using 4 up/down/next key).

setFocusableInTouchMode mainly used for enable/disable 3 view's focus event on touch mode alone.

If 2 you are disabled setFocusable it also disabled the view's 1 focus event on touch mode.

Score: 24

After reading the answer from @Raj, going 52 through the Android documentation, and experimenting 51 with the code, I think I understand how 50 they work a little better now. Here is a 49 little additional help if anyone else is 48 similarly confused. Let me know if I've 47 got it wrong.

Views can be focused or unfocused. Some 46 views change their appearance when they 45 are focused. This can be especially useful 44 when using a keypad to navigate the layout. This 43 way you know where you are before you actually 42 click a view. Even views that do not naturally 41 change when focus can be made to do so by 40 using a selector and drawables. If you are not using a keypad to navigate, though, focus 39 is not as important.

There were three pairs 38 of things that were confusing me:

isFocusable()
isFocusableInTouchMode()

setFocusable()
setFocusableInTouchMode()

requestFocus()
requestFocusFromTouch()

The first 37 pair just tells you information about the 36 view. That is, whether or not it is even 35 possible for that view to be focused. You 34 can find out by doing something like this:

Boolean b = myView.isFocusable();

You 33 are in touch mode after you have touched 32 the screen. So something that may be focusable 31 when you are using the keypad may not be 30 focusable when you are using your fingers. You 29 can check like this:

Boolean b = myView.isFocusableInTouchMode();

Like I said, this only 28 tells you information about whether it is 27 even possible to give the view focus or not. If 26 you want to actually give the view focus, first 25 you have to make it possible to be focused. You 24 can do this with one of the following commands:

myView.setFocusable(true);
myView.setFocusableInTouchMode(true);

If 23 you are in touch mode and you call setFocusableInTouchMode(true) then 22 both myView.isFocusable() and myView.isFocusableInTouchMode() will return true. You don't need 21 to call them both. However, if you only 20 call myView.setFocusable(true) then myView.isFocusableInTouchMode() will not be changed.

Now to finally 19 make the view focused you have to call the 18 following:

myView.requestFocus();

I still don't fully understand 17 requestFocusInTouchMode() because just using requestFocus() worked for me, but 16 the documentation says about requestFocusInTouchMode():

Call this to try to give 15 focus to a specific view or to one of its 14 descendants. This is a special variant of 13 requestFocus() that will allow views that 12 are not focuable in touch mode to request 11 focus when they are touched.

Finally, it 10 should be noted that Romain Guy said in this post:

A view 9 that's focusable in touch mode has weird 8 interactions and unless you perfectly understand 7 what you are doing you should NOT use it. requestFocus() works, but 6 focus is shown only when the device is not 5 in touch mode. As soon as the user touches 4 the screen, the focus is not displayed anymore. By 3 doing what you are doing you are making 2 your app behave differently from the rest 1 of the system and you risk weird behaviors.

More Related questions