Question

I've studied and created some app widgets in android. So I have a widget that there are 6 imageviews and when the user clicks in one of those, the widget will launch the application.

The problem is because the user doesn't have the feedback that his touch was successfull (I mean, the view isn't tinted, because there is no onTouchListener like the others).

I see many widgets which is possible to tint when the user touches, but I can't do that (in remoteviews I can't get the access directly through the view)! How can I make it?

Was it helpful?

Solution

You can achieve this by creating a custom drawable as below and set it to the ImageView. When the user touch on the ImageView show a different color.

widget_item_image.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android">
 <item android:drawable="@color/black_overlay"
    android:state_pressed="true"/>
 <item android:drawable="@color/maroon" android:state_enabled="true"/>
</selector>

widget_layout.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="match_parent"
  android:layout_height="100dp"
  android:background="@color/white">

<ImageView
  android:id="@+id/imageView1"
  android:layout_width="200dp"
  android:layout_height="60dp"
  android:src="@drawable/widget_item_image"
  android:scaleType="fitXY"
  android:layout_margin="10dp"/>
</LinearLayout>

Now your widget provider onUpdate() should set the widget layout as remote view and add a click intent to the ImageView to start the activity

Intent intent = new Intent(context, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(context,0,intent,0);
RemoteViews remoteViews = new RemoteViews(context.getPackageName(),R.layout.widget_layout);
remoteViews.setOnClickPendingIntent(R.id.imageView1,pendingIntent);
appWidgetManager.updateAppWidget(appWidgetId, remoteViews);

Hope this helps.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top