Accessing Layout Items from inside Widget AppWidgetProvider

半城伤御伤魂 提交于 2019-12-04 10:08:39

You can redraw the screen using another layout with only the ImageButton modified. This sounds like a hackish solution but it works for me.

// Your appWidgetProvider class should track which layout it's currently on
private static int layoutId;

// onReceive should be something like this
@Override
public void onReceive(Context context, Intent intent) {
    Bundle bundle = intent.getExtras();

    if (bundle != null) {
        int newLayoutId = bundle.getInt("layout_id");
        // Change the current layout id to the layout id contained in the bundle
        layoutId = newLayoutId;
        initViews(context);
    } else {
        initViews(context);
    }
}

// Initialize the view according to the chosen layout
private void initViews(Context context) {
    RemoteViews views = null;

    // Set the initial layout   
    if (layoutId == 0) {
        Intent layoutIntent = new Intent("android.appwidget.action.APPWIDGET_UPDATE");
        Bundle layoutBundle = new Bundle();

        // I put in the layoutId of the next layout. Note that the layoutId is not
        // from R. I just made up some easy to remember number for my layoutId
        layoutBundle.putInt("layout_id", 1);

        PendingIntent lPendingIntent = PendingIntent.getBroadcast(context, 0,
                layoutIntent, PendingIntent.FLAG_ONE_SHOT);

        // Set the layout to the first layout
        views = new RemoteViews(context.getPackageName(), R.layout.layout_zero);

        // I used buttons to trigger a layout change
        views.setOnClickPendingIntent(R.id.btnNext, lPendingIntent);
    } // Else if there's some trigger to change the layout...
    else if (layoutId == 1) {
        Intent layoutIntent = new Intent("android.appwidget.action.APPWIDGET_UPDATE");
        Bundle layoutBundle = new Bundle();

        // Since I only have two layouts, I put here the id of the previous layout
        layoutBundle.putInt("layout_id", 0);

        PendingIntent lPendingIntent = PendingIntent.getBroadcast(context, 0,
                layoutIntent, PendingIntent.FLAG_ONE_SHOT);

        // Set the layout to the second layout
        // This layout should be almost the same as the first layout except for the
        // part that you want to change
        views = new RemoteViews(context.getPackageName(), R.layout.layout_one);
        views.setOnClickPendingIntent(R.id.btnPrev, lPendingIntent);
    }
}

As you can see, I used two different layouts which the user is able to switch via button clicks. You can use different triggers to change the layout depending on what you need. A similar solution can be found link text. Pardon me for putting the code in onReceive instead of onUpdate :)

@Avendael Your code works perfectly. Only thing you need to do is add update widget. Some thing like this

public static void buildUpdate(Context context) {
RemoteViews updateViews;                    
//Have your own condition here
if (1==1){
    layoutId = R.layout.main;
} else {
    layoutId = R.layout.main1;
}
updateViews = new RemoteViews(context.getPackageName(),
    layoutId);
//actually do things here
//then finally, return our remoteView
AppWidgetManager.getInstance(context).updateAppWidget(
    new ComponentName(context, ButtonWidget.class), updateViews);

}

Call this method buildUpdate(context) after assigning new remote view to update widget layout. For eg we can place this after views.setOnClickPendingIntent(); in the above code. This is really a great approach, was really helpful for me cheers...

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!