I want to know the actual difference between them... When should each be used, how should each be used, and in which situations is each helpful?
Give some examples and explain them in detail.
Focusable in touch mode is a property that you can set yourself either from code or XML. However, it should be used sparingly and only in very specific situations as it breaks consistency with Android normal behavior.
Focusable means that it can gain the focus from an input device like a keyboard. Input devices like keyboards cannot decide which view to send its input events to based on the inputs itself, so they send them to the view that has focus.
So android:focusableInTouchMode="true" means that the view can get the focus when the phone is in touch mode. Typically an EditText is generally focusable in touch mode and on the other hand a Button is generally not focusable in touch mode.
It is explained in the Android Developers Blog: http://android-developers.blogspot.co.at/2008/12/touch-mode.html
The following quotes should make it clear:
By itself, the touch mode is something very easy to understand as it simply indicates whether the last user interaction was performed with the touch screen. For example, if you are using a G1 phone, selecting a widget with the trackball will take you out of touch mode;
...
In touch mode, there is no focus and no selection. Any selected item in a list of in a grid becomes unselected as soon as the user enters touch mode. Similarly, any focused widgets become unfocused when the user enters touch mode.
...
Now that you know focus doesn't exist in touch mode, I must explain that it's not entirely true. Focus can exist in touch mode but in a very special way we call focusable in touch mode. This special mode was created for widgets that receive text input, like EditText or, when filtering is enabled, ListView.
...
Focusable in touch mode is a property that you can set yourself either from code or XML. However, it should be used sparingly and only in very specific situations as it breaks consistency with Android normal behavior. A game is a good example of an application that can make good use of the focusable in touch mode property. MapView, if used in fullscreen as in Google Maps, is another good example of where you can use focusable in touch mode correctly.
Give some example and explain them in detail
I'll give you my own experience:
I had a Google TV application which had an activity with a great deal of ImageButtons.
I wanted the ImageButtons to be selectable.
So if a person clicks on them with mouse or remote controller, they become selected only (Highlighted in my case). Then if the user presses the selected ImageButton
, the action triggers. This exact behaviour was achieved through enabling the focusableInTouchMode
property through the XML layout.
All I had to do was to set an ordinary onClickListener
for the ImageButtons and voila!
I haven't checked my application on handset but I guess it would deliver familiar result.
When?
I've told you a use case I have tested: When you want your Button's onClickListener
to trigger action on your second click, after you have first clicked and selected the Button
.
I Used the first click to gain "focus" and display a Zoom-In scale Up animation on my button.
How?
Just set the button's property focusableInTouchMode
to true
in your XML layout file.
Focused is a state for view and generally focus can be changed with trackball and dpad. Your view can have different background when state is focused.
Focusable in touch mode allows view to gain focus when user is touching the view, good example of this kind of component is EditText
.
With Button
or any clickable component pressed state is usually what you are interested in.
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