Android list widget and item not working
I know that this question has been asked many times, but I tried almost all the solutions, and nothing helped me. So I think I didn’t understand anything.
Here is the point, I have a widget on the main screen, the list is one, and, as usual, I want each element to open activity when clicked.
I see a lot of answers with toasts and broadcasts, but I want him to discover activity.
The fact is that the click does not work.
Here is what I did:
widget_info.xml
<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:minHeight="50dp"
android:minWidth="300dp"
android:previewImage="@mipmap/ic_launcher"
android:resizeMode="horizontal|vertical" />
Provider
class StickyWidgetProvider : AppWidgetProvider() {
override fun onReceive(context: Context, intent: Intent) {
super.onReceive(context, intent)
if (ACTION_APPWIDGET_UPDATE == intent.action) {
updateWidget(context)
}
}
override fun onUpdate(context: Context, appWidgetManager: AppWidgetManager, appWidgetIds: IntArray) {
for (i in 0 until appWidgetIds.size) {
val appWidgetId = appWidgetIds[i]
val rv = RemoteViews(context.packageName, R.layout.widget_container)
rv.setRemoteAdapter(R.id.widget_list, Intent(context, MyWidgetService::class.java))
val activityIntent = Intent(context, MainActivity::class.java)
.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_NEW_TASK)
val pendingIntent = PendingIntent.getActivity(context, 0, activityIntent, PendingIntent.FLAG_UPDATE_CURRENT)
rv.setPendingIntentTemplate(R.id.widget_list, pendingIntent)
appWidgetManager.notifyAppWidgetViewDataChanged(appWidgetId, R.id.widget_list)
appWidgetManager.updateAppWidget(appWidgetId, rv)
}
}
private fun updateWidget(context: Context) {
val appWidgetManager = AppWidgetManager.getInstance(context)
appWidgetManager.notifyAppWidgetViewDataChanged(
appWidgetManager.getAppWidgetIds(
ComponentName(context, StickyWidgetProvider::class.java)
),
R.id.widget_list)
}
}
AND RemoteViewsFactory
class StickyWidgetRemoteViewsFactory(private val context: Context) : RemoteViewsService.RemoteViewsFactory {
private var itemList: List<MyItems>? = null
private fun updateWidgetListView() {
itemList = filled by somrthing
}
override fun onCreate() {
updateWidgetListView()
}
override fun onDataSetChanged() {
updateWidgetListView()
}
override fun onDestroy() {
itemList = null
}
override fun getCount(): Int {
return if (itemList != null)
itemList!!.size
else
0
}
override fun getViewAt(position: Int): RemoteViews {
val remoteView = RemoteViews(context.packageName, R.layout.widget_row)
val item = itemList!![position]
// fill the view //
val bundle = Bundle()
bundle.putParcelable(MainActivity.EXTRA_ITEM, item)
val fillInIntent = Intent().putExtras(bundle)
remoteView.setOnClickFillInIntent(R.id.widget_item, fillInIntent)
return remoteView
}
...
}
I just can't understand what I'm doing wrong here. Following the guide https://developer.android.com/guide I have setPendingIntentTemplate
in Provider
, populated setOnClickFillInIntent
in the factory, but clicking an element does nothing.
LinearLayout
TextViews
, android:textIsSelectable="false"
, android:clickable="false"
android:focusable="false"
, , !
: