Android set focus on a View inside a ScrollView

If you are using a XML layout then you must set the view focusable using android:focusable and android:focusableInTouchMode. If you are using java code to generate layout then you can use methods setFocusable(boolean) and setFocusableInTouchMode(boolean). You can then set the focus to a view using public method requestFocus().
RadioGroup rg = new RadioGroup(this); //create the RadioGroup
rg.setFocusable(true);
rg.setFocusableInTouchMode(true);

boolean myFocus = radio.requestFocus();
Log.w("My Debug Data:", "Focus" + myFocus);

Focus Handling

The framework will handle routine focus movement in response to user input. This includes changing the focus as views are removed or hidden, or as new views become available. Views indicate their willingness to take focus through the isFocusable() method. To change whether a view can take focus, call setFocusable(boolean). When in touch mode (see notes below) views indicate whether they still would like focus via isFocusableInTouchMode() and can change this via setFocusableInTouchMode(boolean).

android:focusable

Boolean that controls whether a view can take focus. By default the user can not move focus to a view; by setting this attribute to true the view is allowed to take focus. This value does not impact the behavior of directly calling requestFocus(), which will always request focus regardless of this view. It only impacts where focus navigation will try to move focus.

Must be a boolean value, either "true" or "false".

This corresponds to the global attribute resource symbol focusable. Related Methods

setFocusable(boolean)

android:focusableInTouchMode

Boolean that controls whether a view can take focus while in touch mode. If this is true for a view, that view can gain focus when clicked on, and can keep focus if another view is clicked on that doesn't have this attribute set to true.

Must be a boolean value, either "true" or "false".

This corresponds to the global attribute resource symbol focusableInTouchMode. Related Methods

setFocusableInTouchMode(boolean)


public final boolean requestFocus (int direction)

Call this to try to give focus to a specific view or to one of its descendants and give it a hint about what direction focus is heading. 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. See also focusSearch(int), which is what you call to say that you have focus, and you want your parent to look for the next one. This is equivalent to calling requestFocus(int, Rect) with null set for the previously focused rectangle.

Parameters
direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
Returns

Whether this view or one of its descendants actually took focus.

public final boolean requestFocus ()

Call this to try to give focus to a specific view or to one of its descendants. 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. See also focusSearch(int), which is what you call to say that you have focus, and you want your parent to look for the next one. This is equivalent to calling requestFocus(int, Rect) with arguments FOCUS_DOWN and null.

Returns

Whether this view or one of its descendants actually took focus.

No comments:

Post a Comment

NO JUNK, Please try to keep this clean and related to the topic at hand.
Comments are for users to ask questions, collaborate or improve on existing.