Android Widgets: How to add Views to RemoteViews on runtime
Asked Answered
A

1

8

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?

Appose answered 15/2, 2012 at 16:42 Comment(0)
A
5

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

Appose answered 24/2, 2012 at 7:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.