Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When using setOnClickFillInIntent and setPendingIntentTemplate, click events never occur

I am working on an Android home screen app widget that fills a listview with custom defined row layouts. The listview gets populated correctly, but when I click any of the items, nothing happens.

Here is some of the relevant parts

From the AppWidgetManager's onUpdate()

...
final Intent contactIntent = new Intent(context, ContactService.class);
contactIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, widgetId);
contactIntent.setData(Uri.parse(contactIntent.toUri(Intent.URI_INTENT_SCHEME)));
view.setRemoteAdapter(widgetId, R.id.listview, contactIntent);
//works fine up to here, populates ListView

//Set up pendingIntentTemplate
final Intent contactClick = new Intent(context,ContactDial.class);
contactClick.setData(Uri.parse(contactClick.toUri(Intent.URI_INTENT_SCHEME)));
final PendingIntent contactClickPI = PendingIntent.getBroadcast(context,
     0,contactClick,PendingIntent.FLAG_UPDATE_CURRENT);
view.setPendingIntentTemplate(R.id.listview, contactClickPI);
Log.d("TEMPLATE", "set template");
...

From the RemoteViewsFactory's getViewAt(int position)

final Intent i = new Intent();
final Bundle extra = new Bundle();
extra.putString("number", phoneNumber);

i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.putExtras(extra);
row.setOnClickFillInIntent(R.id.contact_row,i);
Log.d("FACTORY", "Set Intent Template");

The logs show that the code was executed correctly, but for some reason it doesn't seem to register my clicks. I put a Log.d in the onCreate of the class that the intent is supposed to be sent to, but it never happens. Here are the important parts of my layouts.

The main layout:

<LinearLayout
        android:id="@+id/layout_two"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:descendantFocusability="blocksDescendants" >


        <ListView
            android:id="@+id/listview"
            android:layout_width="match_parent"
            android:layout_height="260dp" />

        <!-- Some other views -->

    </LinearLayout>

The custom defined row:

<LinearLayout android:id="@+id/contact_row"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:background="@xml/buttoncolor_selector"
    android:paddingBottom="2dp"
    android:layout_height="?android:attr/listPreferredItemHeight"
    android:paddingLeft="10dip" >

    <ImageView
        android:id="@+id/contact_photo"
        android:layout_width="@android:dimen/notification_large_icon_width"
        android:layout_height="match_parent"
        android:layout_marginRight="5dip"
        android:src="@drawable/ic_action_person   />

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"  >
        <TextView
            android:id="@+id/contact_name"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:textSize="18sp"
            android:gravity="center_vertical"
            android:singleLine="true"
            android:ellipsize="marquee"
            android:textIsSelectable="false"
            android:focusable="false"
            android:focusableInTouchMode="false"
            android:clickable="false"/>
        <TextView
            android:id="@+id/contact_number"
            android:layout_width="match_parent"
            android:layout_height="0dip"
            android:layout_weight="1"
            android:singleLine="true"
            android:ellipsize="marquee"
            android:textIsSelectable="false"
            android:focusable="false"
            android:focusableInTouchMode="false"
            android:clickable="false"  />
    </LinearLayout>

</LinearLayout>

I've looked at just at just about every example I can find on the Internet, but none of the solutions have changed anything. Please help.

like image 348
Sustenance Avatar asked Dec 31 '25 08:12

Sustenance


1 Answers

In your custom defined row, try moving android:id="@+id/contact_row" down to a child view (even if it's a "useless" viewgroup that just surrounds everything else). Or, surround your top level LinearLayout with a FrameLayout. I didn't have luck having the setOnClickFillInIntent() set to target the ID on the very top level viewgroup like that.

Like so,

<FrameLayout  xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >

    <LinearLayout android:id="@+id/contact_row"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:background="@xml/buttoncolor_selector"
    android:paddingBottom="2dp"
    android:layout_height="?android:attr/listPreferredItemHeight"
    android:paddingLeft="10dip" >

    <ImageView
        android:id="@+id/contact_photo"
        android:layout_width="@android:dimen/notification_large_icon_width"
        android:layout_height="match_parent"
        android:layout_marginRight="5dip"
        android:src="@drawable/ic_action_person"   />
........

Depending on your needs, you may be able to eat your cake and have it too, and have a non-useless viewgroup if you rearrange your layout.

like image 85
Kabliz Avatar answered Jan 01 '26 21:01

Kabliz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!