Search code examples
androidcheckboxmove

Android CheckBox in List to Right


I am creating a list which consists of an image, text, and a check box for each line. I use Table Layout but I want to move checkbox place to right, gravity:right did not work. What can I do for this? Code:

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
    <TableRow>
        <ImageView
            android:id="@+id/img"
            android:layout_width="50dp"
            android:layout_height="50dp"/>

        <TextView
            android:id="@+id/txt"
            android:layout_width="wrap_content"
            android:layout_height="50dp" />

        <CheckBox
            android:id="@+id/checkBox1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="20dp"
            android:text="" 

            />

</TableRow>
</TableLayout>

Solution

  • You will need to set the weight of CheckBox

    So your code now would be:

    <?xml version="1.0" encoding="utf-8"?>
    <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
        <TableRow>
            <ImageView
                android:id="@+id/img"
                android:layout_width="50dp"
                android:layout_height="50dp"/>
    
            <TextView
                android:id="@+id/txt"
                android:layout_width="wrap_content"
                android:layout_height="50dp"
                android:gravity="center_vertical"
                 />
    
            <LinearLayout
            android:id="@+id/linearLayout1"
            android:layout_width="wrap_content"
            android:gravity="right"
            android:layout_weight="0.08"
            android:layout_height="fill_parent" >
    
                <CheckBox
                    android:id="@+id/checkBox1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:gravity="center_vertical" />
    
        </LinearLayout>
    
    </TableRow>
    
    </TableLayout>
    

    Answer given by Jagdeep Singh. Note: I removed the answer from the question so I had to put it here. But I will remove this as soon as Jagdeep writes an answer and I am notified