My app has a simple widget (to show a large image to launch the app) It works fine under Android 28 but it is different with API31 : the thumbnail to drag is present in widgets list, but shown (with emulator) by a blank space, as shown in picture here I noticed ther were changes in Android 31 concerning Widgets, but found nothing explaining this problem
Code elements : Manifest
activity
android:name="com.dom.my_App.myAppAppWidgetConfigureActivity"
android:exported="true" >
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_CONFIGURE" />
</intent-filter>
</activity>
AppWidgetConfigureActivity
public class myAppAppWidget extends AppWidgetProvider {
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
// There may be multiple widgets active, so update all of them
for (int appWidgetId : appWidgetIds) {
updateAppWidget(context, appWidgetManager, appWidgetId);
}
}
@Override
public void onDeleted(Context context, int[] appWidgetIds) {
// When the user deletes the widget, delete the preference associated with it.
for (int appWidgetId : appWidgetIds) {
myAppAppWidgetConfigureActivity.deleteTitlePref(context, appWidgetId);
}
}
@Override
public void onEnabled(Context context) {
// Enter relevant functionality for when the first widget is created
}
@Override
public void onDisabled(Context context) {
// Enter relevant functionality for when the last widget is disabled
}
static void updateAppWidget(Context context, AppWidgetManager appWidgetManager,
int appWidgetId) {
// CharSequence widgetText = myAppAppWidgetConfigureActivity.loadTitlePref(context, appWidgetId);
// Construct the RemoteViews object
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.my_App_app_widget);
// views.setImageViewBitmap (R.id.appwidget_text,
views.setImageViewResource(R.id.widget_imageView, R.drawable.alerte_pm);
Intent intent = new Intent(context, MainActivity.class);
// views.setOnClickPendingIntent(R.id.widget_imageView, getPendingSelfIntent(PendingIntent.getActivity(
PendingIntent pendingIntent = PendingIntent.getActivity( /* context = */ context,
/* requestCode = */ 0,
/* intent = */ intent,
/* flags = */ PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE
);
views.setOnClickPendingIntent(R.id.widget_imageView, pendingIntent);
// Instruct the widget manager to update the widget
appWidgetManager.updateAppWidget(appWidgetId, views);
}
private static PendingIntent getPendingSelfIntent(Context context, String action) {
Intent intent = new Intent(context.getApplicationContext(), MainActivity.class);
//startActivity(intent);
intent.setAction(action);
return PendingIntent.getBroadcast(context, 0, intent, 0);
}
}
App_widget
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@android:id/background"
style="@style/Widget.my_SMS.AppWidget.Container"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppWidgetContainer">
<ImageView
android:id="@+id/widget_imageView"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:background="@drawable/app_widget_inner_view_background"
android:contentDescription="@string/app_name"
android:cropToPadding="false"
android:scaleType="fitCenter"
app:srcCompat="@drawable/alerte_pm" />
</RelativeLayout>
app_wigzt_info
<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:minWidth="80dp"
android:minHeight="80dp"
android:updatePeriodMillis="86400000"
android:previewImage="@drawable/alerte_pm"
android:previewLayout="@layout/my_App_app_widget">
android:installLocation="preferExternal"
android:initialLayout="@layout/my_App_app_widget"
android:description="@string/app_name"
android:configure="com.dom.my_App.myAppAppWidgetConfigureActivity"
android:resizeMode="horizontal|vertical"
android:widgetCategory="home_screen|keyguard"
android:previewLayout="@layout/my_App_app_widget"
android:targetCellHeight="2"
android:targetCellWidth="2"
android:initialKeyguardLayout="@layout/my_App_app_widget"
tools:targetApi="s">
</appwidget-provider>
I have found the solution : it was in styles.xml (V31) : I introduced this item
<item name="android:background">@drawable/my_drawable</item>
In the recent version of Android Studio, the files generated by adding a new activity Widget are OK also for Android API 30.
It was the way to understand the things to do.