Bruno Berisso
Bruno Berisso

Reputation: 1091

android TextView.setText doesn't work on simple widget

I have the extremely simple AppWidgetProvider for a test widget:

public class Test extends AppWidgetProvider {

    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds){
        RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.test_layout);
        views.setTextViewText(R.id.TextView01, "Test message");
    }

}

The test_layout looks like this:

<LinearLayout 
    android:id="@+id/LinearLayout01"
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    xmlns:android="http://schemas.android.com/apk/res/android">
    <TextView
        android:id="@+id/TextView01" 
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
    </TextView>
</LinearLayout>

The problem is that the widget appears in the emulator screen but without any text. I'm sure that's i'm messing something but i can't find what it is...

Upvotes: 3

Views: 4518

Answers (1)

Raunak
Raunak

Reputation: 6507

You forgot to set the RemoteViews to use. Your code for AppWidgetProvider should be this:


public class Test extends AppWidgetProvider {

    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds){
        super.onUpdate(context, appWidgetManager, appWidgetIds);
        RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.test_layout);
        views.setTextViewText(R.id.TextView01, "Test message");
        appWidgetManager.updateAppWidget(appWidgetIds, views);
    }

}

Upvotes: 9

Related Questions