5

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);

3 Answers 3

13

Stumbled upon this question in searching for my own answer, and while this doesn't answer my question i figured i would answer this question.

Assuming you already have a layout file for your widget, test.xml.

Now, create a new layout, save e.g. to text_view_layout.xml. In that layout xml have this as its contents:

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="TextView"
    android:textAppearance="?android:attr/textAppearanceLarge" />

You now just created a layout with its root view being a text view.

Now in your code you can add text to this text view like so:

RemoteViews update = new RemoteViews(getPackageName(), R.layout.test);
for(int i = 0; i < 3; i++) {
    RemoteViews textView = new RemoteViews(getPackageName(), R.layout.text_view_layout);
    textView.setTextViewText(R.id.textView1, "TextView number " + String.valueOf(i));
    update.addView(R.id.linearLayout1, textView);
}

mAppWidgetManager.updateAppWidget(mAppWidgetId, update);

Now you just created three textViews all with the text being "TextView number 0" and so on...

I'm sure there is a similar answer somewhere else, but this is how to programmatically add textViews to an appWidget.

RemoteViews API

4

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

0

You could try this

LinearLayout l = new LinearLayout(_context);

for (int i = 0; i < 10; i++) {
 TextView t = new TextView(this);
 t.setText("Hello");
 t.setBackgroundColor(Color.RED);
 t.setSingleLine(true);
 l.addView(t); 
 }

l.setId(100)

RemoteViews views = new RemoteViews(context.getPackageName(),100);
views.setTextViewText(R.id.widget_control1, value1);
views.setTextViewText(R.id.widget_control2, value2);
4
  • Thanks, I edited my question a little, my point is how should I handle RemoteViews, since it seems to manage widget layout. If I call setContentView(l), should I also call RemoteViews somewhere?
    – kkgery
    Feb 13, 2012 at 19:40
  • 1
    It doesn't seem to work. SetContentView is defined for Activities class and not for AppWidgetProvider. And if I call it from the widget Activity class, it creates a new window (activity) and populates the controls there. So do I need to call RemoteViews from the Update method of a class extending AppWidgetProvider? Or there is a way to use setcontentview somehow?
    – kkgery
    Feb 14, 2012 at 14:18
  • new RemoteViews(context.getPackageName(), R.layout.my_widget); Here R.layout.my_widget is an int value. Now what you can do is design a RelativeLayout (or whatever you prefer) and give it an id (example myLayout.setId(100)). Now you can use this id (100 in this example) in place of R.layout.my_widget Feb 14, 2012 at 22:46
  • Great suggestion, however, my widget does not show anything with that scheme. Does it work for you? I also tried to assign the XML defined linearlayout id to l, and played with getId() for l and t's but with no luck. Seems like RemoteViews has some problems with passing a View id to it as a parameter.
    – kkgery
    Feb 15, 2012 at 8:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.