Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Widgets: How to add Views to RemoteViews on runtime

I want to add text views arranged in a linear layout to my widget on runtime. I do following:

LinearLayout l = new LinearLayout(context);
for (int i = 0; i < 10; i++) {
    TextView t = new TextView(context);
    t.setText("Hello");
    l.addView(t);  }        
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.main);
RemoteViews view = new RemoteViews (context.getPackageName(), l.getId());
views.addView(R.layout.main, view);

But when I add widget I get a Problem loading widget error. Seems like RemoteViews has a problem with receiving a constructed view id as a parameter. But I can't make a reference to XML resource, because they are created on runtime. What is the proper way to populate the RemoteViews with TextViews on runtime?

like image 300
kkgery Avatar asked Feb 15 '12 16:02

kkgery


People also ask

Is view a widget in Android?

View is the base class for widgets, which are used to create interactive UI components (buttons, text fields, etc.). The ViewGroup subclass is the base class for layouts, which are invisible containers that hold other View s (or other ViewGroup s) and define their layout properties.

What are remote views Android?

RemoteViews describes a view hierarchy that can be displayed in another process. The hierarchy is inflated from a layout resource file, and this class provides some basic operations for modifying the content of the inflated hierarchy. The main reason behind this choice is related to security and performance.

How do you update an app widget?

Full update: Call AppWidgetManager. updateAppWidget(int, android. widget. RemoteViews) to fully update the widget.


1 Answers

Only xml resources can be used in RemoteViews. Views created on runtime should be based on predefined xml views.

like image 156
kkgery Avatar answered Sep 30 '22 22:09

kkgery