Search code examples
androidlayoutwidgetremoteview

Adding TextViews to home screen widget programmatically


I want to programmatically add Text Views controls to my home screen widget. In the following example I populate Linearlayout with TextViews, but how should I use RemoteViews here? It only accepts xml resource layout as a parameter.

public class MyWidget extends AppWidgetProvider {
    public void onUpdate(Context _context, AppWidgetManager appWidgetManager, 
                         int[] appWidgetIds) {

        LinearLayout l = new LinearLayout(_context);

        for (int i = 0; i < 10; i++) {
            TextView t = new TextView(_context);
            t.setText("Hello");
            l.addView(t); 
        }
    }
}

All tutorials I saw explicitly populate RemoteViews object with values for its predefined controls. And I want to add controls programmaticaly.

RemoteViews views = new RemoteViews(context.getPackageName(),
R.layout.my_widget);
views.setTextViewText(R.id.widget_control1, value1);
views.setTextViewText(R.id.widget_control2, value2);

Solution

  • Ok, It is impossible for appwidgets. Only xml resources are accepted.