Im currently working on Andorid 12, with min sdk version as 31 in both host and widget provider application. From AppWidgetProviderInfo, I got the layout id of previewLayout but I'm trying to display the same in host customisation screen. While trying to do the same I got ResourcesNotFoundException in host implementation.
I added the preview layout in my AppWidgetProviderInfo.xml
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:initialLayout="@layout/widget_initial_layout"
android:previewLayout="@layout/widget_preview_layout"
android:previewImage="@drawable/widget_preview_image"
android:minWidth="130dp"
android:minHeight="170dp"
android:updatePeriodMillis="86400000"
android:widgetCategory="home_screen" />
My host adaptation of preview layout is,
Drawable previewDrawable = widgetInfo.getAppWidgetInfo().loadPreviewImage(binding.getRoot().getContext(), ICON_DENSITY);
int previewLayout = widgetInfo.getAppWidgetInfo().previewLayout;
try {
if (previewLayout != 0) {
View widgetPreview = LayoutInflater.from(binding.getRoot().getContext()).inflate(previewLayout, binding.getRoot(), false);
binding.widgetContainer.addView(widgetPreview);
} else if (previewDrawable != null) {
Glide.with(binding.getRoot().getContext())
.load(previewDrawable)
.into(binding.previewImage);
}
}
catch (Exception e)
{
e.printStackTrace();
}
With the above code, Im able to show the preview image from AppWidgetProviderInfo but while trying to inflate the previewLayout with layout id alone, I get ResourcesNotFoundException. In mycase the widgetprovider and Host are two different applications.