I am developing a launcher, now I am working with app widgets, I follow the tutorial from here: AppWigetHost tutrial - Leonardo Fischer Everything goes well until I tried adding onLongCick listener on the AppWidgetHostView
private void createAppWidget(Intent data) {
Bundle extras = data.getExtras();
final int appWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID);
AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
AppWidgetHostView hostView = mAppWidgetHost.createView(getApplicationContext(), appWidgetId, appWidgetInfo);
hostView.setAppWidget(appWidgetId, appWidgetInfo);
LinearLayout.LayoutParams params = createLayoutParams(appWidgetInfo);
rootLayout.addView(hostView, params);
hostView.setOnLongClickListener(new OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
Toast.makeText(getApplicationContext(), "Long click " + appWidgetId, Toast.LENGTH_LONG).show();
return true;
}
});
}
When I added a widget on the host (The default Google Analog Clock) - and tried to long click on it, it just doesn't work ! After the long click it just opens the Clock application (default action for click).
Can anyone tell me how to fix it ? Here is my full code: http://pastebin.com/61TkuLvx
Added
I've just checked again:
----If I long click on the bound of the Clock widget, the longClick event is fired !
----I added my custom appwidget (which has no onclick handler): so the longclick event is always fired properly.
So I guess, it must have something with touchevent handler/dispatcher to do.
Added I've just tried to set onLongClick on all children of the hostview like this:
private void createAppWidget(Intent data) {
...
...
setChildrenViewLongClick(hostView, new OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
Toast.makeText(getApplicationContext(), "Long click " + appWidgetId, Toast.LENGTH_LONG).show();
return true;
}
});
}
protected void setChildrenViewLongClick(View view, OnLongClickListener listener) {
view.setOnLongClickListener(listener);
String name = view.getClass().getName();
Logger.logInfo("Classname: " + name);
if ( view instanceof ViewGroup ) {
ViewGroup vg = (ViewGroup) view;
for(int i = 0 ; i < vg.getChildCount() ; i++ ) {
setViewLongClick(vg.getChildAt(i), listener);
}
}
}
It just doesn't work :(
The weird thing is: I did another experiment on a another activity, which has a Linearlayout LL( has onLongClick handler ) contains 2 buttons bt1,bt2 - (each button has onClick handler) And apply the method setChildrenViewLongClick(LL,onLongClickListener)- then long click on bt2 - Woop, it works, the action inside onLongClickListener is called !
So which is the problem ? The AppWidgetHostView or the event/handler dispatcher ? Or me (my codes)
I am so confused, please help me !
Tks