How to properly use ListView in AppWidget using Kotlin or Java?

802 Views Asked by At

My goal is to build a scrollable home screen widget. I'm trying to use Kotlin, but Java solutions are welcome too. Every documentation I found online was either outdated or did not include scrolling. ScrollView does not seem to be supported, so I tried to use a ListView. As far as I know you can only add content to ListViews programmatically. I tried

class NewAppWidget : AppWidgetProvider() {
    override fun onUpdate(context: Context, appWidgetManager: AppWidgetManager, appWidgetIds: IntArray) {
        for (appWidgetId in appWidgetIds) {
            updateAppWidget(context, appWidgetManager, appWidgetId)
        }
    }
}

internal fun updateAppWidget(context: Context, appWidgetManager: AppWidgetManager, appWidgetId: Int) {
    val views = RemoteViews(context.packageName, R.layout.new_app_widget)
    views.addView(R.id.listView, RemoteViews(context.packageName, R.layout.new_app_widget_list))
    appWidgetManager.updateAppWidget(appWidgetId, views)
}

but my widget always looks like this: "problem loading widget"

1

There are 1 best solutions below

0
Furkan Şenocak On

You should check offical documents.

https://developer.android.com/guide/topics/appwidgets#collections

You dont have to add programmatically.Add listview to layout from xml and set it with

remoteviews.setRemoteAdapter(R.id.yourlistview, intent)

Further information is available in upper link.