2

I want to get the appwidget layout associated with an appwidget id.

In the Widget provider class for an appwidget, the android docs provide an easy way to get the appwidget ids...

public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
    final int N = appWidgetIds.length;
    // Perform this loop procedure for each App Widget that belongs to this provider
    for (int i=0; i<N; i++) {
        int appWidgetId = appWidgetIds[i];


But they IDs dont allow me to know which appwidget provider it comes from (multiple widget providers and layouts.).

EDIT: i am using a service class to update the widgets, not the widget provider class.

in the service class, i want to get the appwidget layout for each id so that i can use remoteviews to update that layout. I have tried a few different suggestions, but none of them give me the value that i need to use with remoteviews. eg "R.id.widget_layout"

How can i get a value like "R.id.widget_layout" from an appwidget ID in the correct "int" form to use with remoteviews?

2 Answers2

5

You can get the AppWidget layout ID and info about the provider for each AppWidget this way:

@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
    final int N = appWidgetIds.length;

    for(int i=0; i<N; i++) {

        int appWidgetId = appWidgetIds[i];

        // you can use layoutId variable instead of R.id.widget_layout
        int layoutId = appWidgetManager.getAppWidgetInfo(appWidgetId).initialLayout;
        // String yourProvider will be something like .WidgetProvider2x2
        String yourProvider = appWidgetManager.getAppWidgetInfo(appWidgetId).provider.getShortClassName();

    }

}
janzoner
  • 1,420
  • 1
  • 11
  • 19
  • Hi @Janzoner, this solution didnt work for me because it only returns the widget id number, and i also need to know which widget layout provider, because i have more than one widget provider. AFAIK remoteviews needs to know which widget provider as well as the widget ID int. –  Apr 26 '14 at 13:49
  • @kodintent This solution works fine, I have tested it on my own widget and works ok. I provided you solution which you needed - layoutId for the widget (not widget id number as you mentioned in the comment). I am going to update my answer and then there will be the information about your provider too. I hope you accept my answer or at least upvote it. – janzoner Apr 26 '14 at 14:50
  • Hi @janzoner im trying to test your code: the widget provider class extends appwidgetmanager and brings the ids with it. but im using an update service class, how can i get the widget ids for all widgetprovider classes in a service class? –  Apr 28 '14 at 12:47
  • 1
    @kodintent I think that you do not need an answer because you have accepted one. – janzoner Apr 28 '14 at 20:28
  • This worked like a charm. That is exactly what I was searching for. That *AppWidgetInfo* provides all the meta data from the provider xml. – Yoraco Gonzales May 01 '17 at 11:58
-1

Because i have more than one widget provider class:

  • I have a separate set of code to update the widgets for each provider. I couldnt find a way to have one set of code update all widgets for all widget providers (but i continue to believe it is possible).

Im using a service class to update the widgets, so i am using "ComponentName" to get the widgetIds for each widget provider class.

My solution is this, repeated for each widget provider class:

// update all 2x2 widget instances
ComponentName thisWidget2x2 = new ComponentName(getBaseContext(), WidgetProvider2x2.class);
int[] allWidgetIds2x2 = localAppWidgetManager.getAppWidgetIds(thisWidget2x2);
for (int widgetId : allWidgetIds2x2) {
    RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.widget_layout_2x2);
    remoteViews.setImageViewResource(R.id.imageviewA_B, timePhraseA_B);
    remoteViews.setOnClickPendingIntent(R.id.topLayout, openAwcPi);
    localAppWidgetManager.updateAppWidget(widgetId, remoteViews);
}

This is repeated for each widget provider

  • hi, downvoter! how about some information - example - why my answer is not good. we all want to know the best way to do something. if its not the best way, you can say why, or even give your own answer. how about it? –  May 07 '14 at 15:46