Changing the visibility of widgets when pressed

My widget consists of two relative layouts. I made both layouts clickable. The following is the layout id:

android:id="@+id/upper_layout" android:id="@+id/bottom_layout" 

Now I need that if the user clicks on upper_layout, bottom_layout should be invisible.

Here is what I have tried so far, but it does not work. Can you check what I am doing wrong? Or perhaps suggest other ways to achieve this.

The code:

 public class BobsWidget extends AppWidgetProvider { public static String ACTION_WIDGET_RECEIVER = "Clicked"; @Override public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.main); Intent active = new Intent(context, BobsWidget.class); active.setAction(ACTION_WIDGET_RECEIVER); PendingIntent actionPendingIntent = PendingIntent.getBroadcast(context, 0, active, 0); remoteViews.setOnClickPendingIntent(R.id.upper_layout, actionPendingIntent); appWidgetManager.updateAppWidget(appWidgetIds, remoteViews); } @Override public void onReceive(Context context, Intent intent) { // TODO Auto-generated method stub // check, if our Action was called if (intent.getAction().equals(ACTION_WIDGET_RECEIVER)) { RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.main); remoteViews.setViewVisibility(R.id.bottom_layout, View.INVISIBLE); } super.onReceive(context, intent); } } 
+7
source share
2 answers

You have several built-in widget functions available in versions of Anndroid 3.0 or later. Check this link

+2
source

I think you forgot to update the widget. Have you tried something like this?

 remoteViews.setViewVisibility(R.id.bottom_layout, View.INVISIBLE); final ComponentName provider = new ComponentName(context, this.getClass()); appWidgetManager.updateAppWidget(provider, views); 
+1
source

All Articles