views:

375

answers:

1

I'm writing an app that will host widgets. The app has custom view (which probably is the source of issue). I obtain AppWidgetHostView like this

private AppWidgetHostView widget;
...
AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
widget = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo); 
widget.setAppWidget(appWidgetId, appWidgetInfo);
mView.addWidget(widget, appWidgetInfo);

mView.addWidget() basically just remembers this AppWidgetHostView instance and then mView draws it directly onto canvas. Visually everything is fine - I can see the actual widget. But the issue is with reacting on UI events. Please advise what needs to be done in the parent view in order to correctly trigger handlers in the widgets like onClick().

Notes:

  1. I used standard widgets which normally react on click events. None worked.

  2. I also created my own test widget with listener (via views.setOnClickPendingIntent(R.id.appwidget_text, pending);) and onClick() is successfully triggered if the widget is added on Homescreen, but doesn't work in my app.

  3. mView correctly detects click event and I tried to call widget.performClick() there, which returns false meaning onClickListener is not registered in the widget. But according to source mAppWidgetHost.createView() would call updateAppWidget which would register its onClick listener..

Please advise where to look at. Thanks

A: 

If your widget doesn't belong to a real ViewGroup that belongs to the system, you need to do a setLocation() on the touch events to align them with where the widgets should be. If you don't, the View code for finding which View was touched won't work as the X and Y values would be wrong (They should be relative to the parent View, not absolute).

Moncader