2

I'm starting with widgets and got a very nice tutorial on the internet, got the example run perfectly, but when i tried to change somethings I got stuck.

The thing is: I just want to change the image from my imageButton when i press it, I've tried somethings but nothing seems to work. I didn't get how the RemoteView and Intent works exactly. So if someone can explain it shortly I would appreciate it =)

Here's the code:

public class HelloWidget extends AppWidgetProvider {
    private ImageButton wifi;
    public static String ACTION_WIDGET_CONFIGURE = "ConfigureWidget";
    public static String ACTION_WIDGET_RECEIVER = "ActionReceiverWidget";

    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
        RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.main);
        Intent configIntent = new Intent(context, ClickOneActivity.class);
        configIntent.setAction(ACTION_WIDGET_CONFIGURE);
        Intent active = new Intent(context, HelloWidget.class);
        active.setAction(ACTION_WIDGET_RECEIVER);
        PendingIntent actionPendingIntent = PendingIntent.getBroadcast(context, 0, active, 0);
        PendingIntent configPendingIntent = PendingIntent.getActivity(context,  0, configIntent, 0);
        remoteViews.setOnClickPendingIntent(R.id.button_wifi, actionPendingIntent);
        remoteViews.setOnClickPendingIntent(R.id.button_two, configPendingIntent);
        appWidgetManager.updateAppWidget(appWidgetIds, remoteViews);
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        // v1.5 fix that doesn't call onDelete Action
        final String action = intent.getAction();
        if (AppWidgetManager.ACTION_APPWIDGET_DELETED.equals(action)) {
            final int appWidgetId = intent.getExtras().getInt(
               AppWidgetManager.EXTRA_APPWIDGET_ID, 
               AppWidgetManager.INVALID_APPWIDGET_ID);

            if (appWidgetId != AppWidgetManager.INVALID_APPWIDGET_ID) {
                this.onDeleted(context, new int[] { appWidgetId });
            }

        } else {
            // check, if our Action was called
            if (intent.getAction().equals(ACTION_WIDGET_RECEIVER)) {
                Toast.makeText(context, "Teste", Toast.LENGTH_LONG).show();
                RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.main);
                remoteViews.setInt(R.id.button_wifi, "toogleOnOff", R.drawable.icon);

            }
            super.onReceive(context, intent);
        }

    }
}

There's a lot of the tutorial code i got as you can see =p

Thx since now

JPM
  • 9,077
  • 13
  • 78
  • 137
Markissimo
  • 309
  • 2
  • 9
  • 23

1 Answers1

1

Looks like you need to understand a little more about RemoteViews. When you call functions like setOnClickPendingIntent, setInt, etc. on the RemoteViews object it basically just stores these operations and arguments internally. Then when the widget is displayed it just plays those operations back to construct the widget's views. So after giving the RemoteViews to the AppWidgetManager by calling updateAppWidget, you can't change them again unless you rebuild the whole RemoteViews and call updateAppWidget again.

As an answer to your question, you want to use a state list as the background for the button. There's a good example here.

TheMike
  • 11
  • 1