Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change widget visibility on click

My widget consists two relative layouts. I have made both the layouts clickable. Following are the id's of the layout:

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

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

Here, is what I have tried so far but it's not working. Can u check what I am doing wrong? Or, maybe suggest some other ways to achieve this.

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);
    }

}
like image 237
curiousguy Avatar asked Feb 24 '12 13:02

curiousguy


2 Answers

You have several inbuilt widget features available in Anndroid 3.0 or more versions.Check this link

like image 124
YshakPK Avatar answered Sep 23 '22 03:09

YshakPK


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);
like image 20
Goo Avatar answered Sep 21 '22 03:09

Goo