androidkotlinwidgetandroid-framelayout

Widget can't load, layout with Famelayout and personal view


I added a widget to my app:

Layout

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@+id/surface">

    <rpt.tool.pongclock.utils.view.PongTimeView
        android:id="@+id/pongview"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"/>
</FrameLayout>

Class

class PongClockWidget : AppWidgetProvider() {

    private var pongThread: PongThread? = null

    override fun onUpdate(
        context: Context,
        appWidgetManager: AppWidgetManager,
        appWidgetIds: IntArray
    ) {
        // There may be multiple widgets active, so update all of them
        for (appWidgetId in appWidgetIds) {
            updateAppWidget(context, appWidgetManager, appWidgetId,pongThread)
        }
    }

    override fun onEnabled(context: Context) {
        // Enter relevant functionality for when the first widget is created
    }

    override fun onDisabled(context: Context) {
        // Enter relevant functionality for when the last widget is disabled
    }
}

@SuppressLint("RemoteViewLayout")
internal fun updateAppWidget(
    context: Context,
    appWidgetManager: AppWidgetManager,
    appWidgetId: Int,
    pongThreadWidget: PongThread?
) {

    // Construct the RemoteViews object
    var pongThread = pongThreadWidget
    val views = RemoteViews(context.packageName, R.layout.pong_clock_widget)
    val inflated = views.apply(context, FrameLayout(context));
    pongThread = ((inflated as FrameLayout).getChildAt(0) as
            rpt.tool.pongclock.utils.view.PongTimeView).thread


    // Instruct the widget manager to update the widget
    appWidgetManager.updateAppWidget(appWidgetId, views)
}

Manifest

<receiver
            android:name=".ui.widget.PongClockWidget"
            android:exported="false">
            <intent-filter>
                <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
            </intent-filter>

            <meta-data
                android:name="android.appwidget.provider"
                android:resource="@xml/pong_clock_widget_info" />
        </receiver>

But when I click on the app icon on the device and add the widgets I only get a white screen with "Widget can't load" written on it, did I forget something?

I tried to launch the widget without the code I added but it still doesn't load.


Solution

  • You can only use a few built-in platform View subclasses in an app widget.

    In particular, you cannot use custom View classes of your own. The app widget is being rendered by the launcher app, not you, and the launcher app does not have access to your custom View.