Search code examples
androidwidgetremoteview

Android widgets: addView not working with RemoteViews object


I want to add textviews to my widget on runtime. I saw many tutorials where RemoteViews object calls addView method to add a view to some layout.

RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.main);
TextView t = new TextView(context);
views.addView(R.id.view_container, t);
appWidgetManager.updateAppWidget(thisWidget, views);

But addView method shows this error: The method addView(int, RemoteViews) is undefined for the type RemoteViews.True, this method is defined for the ViewGroup class, but why is it working for everyone then? And is there a way to add textviews to some RemoteViews on runtime?


Solution

  • Isn't the signature RemoteViews.addView(int, RemoteViews)?

    You seem to have a TextView as the second parameter.

    Something like this should work:

    RemoteViews remoteViewToAdd = new RemoteViews(context.getPackageName(), LAYOUT_ID);
    ...
    views.addView(R.id.view_container, remoteViewToAdd);