Reputation: 134
I am working on an Android Launcher application. I can host AppWidgets and display them on my View, which is derived from a LinearLayout. I can't figure out how to set focus to the buttons within the widget (for example, the individual controls of the Power Control widget), as happens with the default launcher.
This is how I am assigning the focus to the AppWidgetHostView:
widgetView.setFocusable(true);
widgetView.setSelected(true);
widgetView.requestFocus(true);
The entire widget has the focus. I know this because the entire widget flashes if I click the dpad enter button.
How do I set the focus to one of the buttons within the widget? Do I need to enumerate the RemoteViews of the widget? If so, how?
Upvotes: 2
Views: 1745
Reputation: 134
I figured it out. (Thank you HierarchyViewer). If the AppWidgetHostView first child is a ViewGroup (e.g. LinearLayout), I remove focus from the widget and use the FocusFinder to assign focus to the first child.
View view = getView(); // from AppWidgetHost().createView
ViewGroup hostView;
if ( !(view instanceof ViewGroup) )
return; // hostView does not a child widget!
hostView = (ViewGroup) view;
View widget = hostView.getChildAt(0);
if ( !(widget instanceof ViewGroup) )
return; // widget does not have children
hostView.setFocusable(false);
hostView.setSelected(false);
// select first child
FocusFinder focusFinder = FocusFinder.getInstance();
View nextView = focusFinder.findNextFocus(hostView, null, View.FOCUS_RIGHT);
if (nextView != null) {
nextView.setSelected(true);
nextView.requestFocus();
}
Upvotes: 3