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?
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.
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.
Full update: Call AppWidgetManager. updateAppWidget(int, android. widget. RemoteViews) to fully update the widget.
Only xml resources can be used in RemoteViews. Views created on runtime should be based on predefined xml views.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With