Android - ListView items inside app widget not selectable

95 Views Asked by At

I have built a collection app widget for my Android app. The widget consists of a toolbar and then a ListView of tasks (it's a todo-list). The problem is the ListView doesn't show dividers between items and also the items themselves aren't selectable.

My code is as follows:

My agenda_app_widget.xml

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

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/white"
        android:orientation="vertical">

        <RelativeLayout
            android:id="@+id/widget_toolbar"
            android:layout_width="match_parent"
            android:layout_height="?android:attr/actionBarSize"
            android:background="@color/colorPrimary">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentStart="true"
                android:layout_centerVertical="true"
                android:layout_marginStart="16dp"
                android:text="@string/tasks"
                android:textAppearance="@android:style/TextAppearance.DeviceDefault.Widget.ActionBar.Title"
                android:textColor="@color/white" />

            <ImageView
                android:id="@+id/addTaskIv"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentEnd="true"
                android:layout_centerVertical="true"
                android:layout_gravity="center"
                android:layout_marginEnd="16dp"
                android:contentDescription="@string/add_task"
                android:src="@drawable/ic_add_black_24dp"
                android:tint="@color/white" />

        </RelativeLayout>

        <ListView
            android:id="@+id/tasksLView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:descendantFocusability="blocksDescendants"/>

    </LinearLayout>
</FrameLayout>

Here's tasks_widget_list_item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingStart="16dp"
    android:paddingEnd="16dp"
    android:id="@+id/task_list_item_main">

    <TextView android:id="@+id/tvTaskTitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingBottom="4dp"
        android:textColor="@color/black" />

    <TextView android:id="@+id/tvTaskHour"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingBottom="4dp"
        android:textColor="@color/black" />

    <TextView android:id="@+id/tvTaskDay"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingBottom="4dp"
        android:textColor="@color/black" />

</LinearLayout>

And the source code WidgetDataProvider.java

public class WidgetDataProvider implements RemoteViewsService.RemoteViewsFactory {

    private Context mContext;
    private List<Task> mTasks;
    private TaskDataSource mTaskDao;

    public WidgetDataProvider(Context context) {
        mContext = context;
    }

    @Override
    public void onCreate() {
        mTaskDao = new TaskDataSource(mContext);
        mTaskDao.open();
        mTasks = mTaskDao.getAllTasks();
        mTaskDao.close();
    }

    @Override
    public void onDataSetChanged() {
        mTaskDao.open();
        mTasks = mTaskDao.getAllTasks();
        mTaskDao.close();
    }

    @Override
    public void onDestroy() {}

    @Override
    public int getCount() {
        return mTasks.size();
    }

    @Override
    public RemoteViews getViewAt(int position) {
        RemoteViews remoteViews = new RemoteViews(mContext.getPackageName(),
                R.layout.tasks_widget_list_item);

        Task task = mTasks.get(position);

        remoteViews.setTextViewText(R.id.tvTaskTitle, task.getTitle());
        remoteViews.setTextViewText(R.id.tvTaskDay, DateFormat.getDateInstance().format(task.getDay()));
        remoteViews.setTextViewText(R.id.tvTaskHour, new SimpleDateFormat("HH:mm", Locale.getDefault()).format(task.getHour()));

        return remoteViews;
    }

    @Override
    public RemoteViews getLoadingView() {
        return null;
    }

    @Override
    public int getViewTypeCount() {
        return 1;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }
}

How can I fix that?

Thank you.

1

There are 1 best solutions below

0
On

To show a divider in your ListView use

android:dividerHeight="4px"
android:descendantFocusability="blocksDescendants"

And to make items clickable in your ListView

android:clickable="true"

So your final ListView may look like this

<ListView
    android:id="@+id/tasksLView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:divider="#FFCC00"
    android:dividerHeight="4px"
    android:clickable="true"
    android:descendantFocusability="blocksDescendants"/>

You might consider adding clickable attribute in your tasks_widget_list_item too.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingStart="16dp"
    android:paddingEnd="16dp"
    android:clickable="true"
    android:id="@+id/task_list_item_main">

    <!-- .... Other views -->
</LinearLayout>