Search code examples
androidandroid-layoutlistviewandroid-checkbox

Align Checkbox to center of the ListView item


The following layout is used in my app to show cliplists.

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

    <TextView
        android:id="@+id/ClipNo"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="Clip No"
        android:textSize="20sp"
        android:gravity="center"
        android:layout_weight="1"/>

    <TextView
        android:id="@+id/ClipTime"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="Time"
        android:textSize="20sp"
        android:gravity="center"
        android:layout_weight="1"/>

    <TextView
        android:id="@+id/ClipDate"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="Date"
        android:textSize="20sp"
        android:gravity="center"
        android:layout_weight="1"/>

    <CheckBox android:id="@+id/ClipMark"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_weight="1" />

</LinearLayout>

The below image shows the output of the layout

enter image description here

How can i arrange the 'CheckBox' to center of the 'Mark' Header

Thanks in advance


Solution

  • Because you have added android:layout_weight="1"

    Change your below code

     <CheckBox 
            android:id="@+id/ClipMark"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_weight="1" />
    

    To

    <LinearLayout
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:layout_weight="1">
    
            <CheckBox 
                android:id="@+id/ClipMark"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"/>
    
    </LinearLayout>