Android clickable Widget with ListView not clickable on ListItems
Asked Answered
L

1

7

I am having trouble making my Widget clickable. The following code is working partially. My Widget shows a ListView with Items. When clicking into the Widget where no Item is displayed, the Intent works and the Activity starts. But when clicking on an Item of the ListView nothing happens.

Here is the Code: WidgetProvicer:

public void onUpdate(final Context context, final AppWidgetManager appWidgetManager, final int[] appWidgetIds) {


    for (final int iD : appWidgetIds) {

        final RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout
                .widget_layout);

        final Intent intent = new Intent(context, TickWidgetService.class);
        intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, iD);
        intent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME)));
        remoteViews.setRemoteAdapter(R.id.WidgetItem, intent);

        remoteViews.setEmptyView(R.id.WidgetItem, R.id.empty_view);

        final Intent activityIntent = new Intent(context, MainActivity.class);
        final PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, activityIntent, 0);

        remoteViews.setOnClickPendingIntent(R.id.Widget, pendingIntent);


        appWidgetManager.updateAppWidget(iD, remoteViews);
        appWidgetManager.notifyAppWidgetViewDataChanged(iD, R.id.WidgetItem);
    }

    super.onUpdate(context, appWidgetManager, appWidgetIds);
}

widget_layout.xml:

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:id="@+id/Widget"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:orientation="vertical">
 <ListView
    android:id="@+id/WidgetItem"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:divider="#D3D3D3"
    android:dividerHeight="1dp"/>


 <TextView
    android:id="@+id/empty_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/darker_gray"
    android:gravity="center"
    android:text="@string/emptytext"
    android:visibility="gone"/>
 </LinearLayout>

The ListViewItems have no Buttons or anything.

Thanks for your help!

Lenoir answered 30/8, 2015 at 10:53 Comment(2)
Have you tried to just make listview items not clicable? Because i think they catch click events. By overriding areAllItemsEnabled() in listview adapter or sth?Syrup
I am not using an Adapter. Ii have a RemoteViewsFactory. In the getViewAt() Method I add the content to the RemoteView, a RemoteView is one ListItem.Lenoir
L
3

finally it is working:

in the Profider onUpdate:

final Intent activityIntent = new Intent(context, MainActivity.class);
final PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, activityIntent, PendingIntent.FLAG_UPDATE_CURRENT);
remoteViews.setPendingIntentTemplate(R.id.WidgetItem, pendingIntent);

and in the ViewsFactory getViewAt:

Bundle infos = new Bundle();
infos.putInt(TickWidgetProvider.WIDGET_LISTID, this.listId);
final Intent activityIntent = new Intent();
activityIntent.putExtras(infos);

remoteView.setOnClickFillInIntent(R.id.widgetListItemLayout, activityIntent);

Do not know where I found it, but this example helped me making it work: https://github.com/commonsguy/cw-advandroid/tree/master/AppWidget/LoremWidget

Lenoir answered 7/10, 2015 at 0:59 Comment(1)
I made it work by catching the click event along the intent in MainActivity. Is there any way to receive that on onReceive()? I tried but didn't work. :(Stated

© 2022 - 2024 — McMap. All rights reserved.