Android TextView.setText does not work with simple widgets

I have a very 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");
    }

}

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 on the emulator screen, but without text. I'm sure I'm confusing something, but I can’t find what it is.

+3
source share
1 answer

You forgot to install RemoteViews to use. Your code for AppWidgetProvider should be as follows:


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

}

+9
source

Source: https://habr.com/ru/post/1777187/


All Articles