Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I align checkbox with text to right side of the screen?

I have a checkbox inside a TableRow. I would like to align both checkbox and text to the right side of the screen but when I use android:gravity="right" it only aligns the text to the right side of the screen but not the square (the checkbox itself). It seems that they are aligned separately (checkbox on the left and text on the right side of the screen).

Here is the snippet of xml that refers to the checkbox:

<TableRow android:layout_height="wrap_content"
          android:layout_width="match_parent"
          android:layout_marginBottom="10dp">

          <CheckBox
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Select"
                android:id="@+id/checkboxSelect"
                android:layout_column="0"
                android:layout_weight="1"
                android:gravity="right"/>
</TableRow>

How can I align both the checkbox and the text on it to the right side of the screen?

Thanks in advance!

like image 651
Francisco Romero Avatar asked Dec 04 '25 21:12

Francisco Romero


2 Answers

Simply add Checkbox as below.

<?xml version="1.0" encoding="utf-8"?>
<CheckBox xmlns:android="http://schemas.android.com/apk/res/android"
    android:text="hello"
    android:layout_width="match_parent" 
    android:layout_height="match_parent"
    android:button="@null"
    android:drawableRight="?android:attr/listChoiceIndicatorMultiple"/>

It will show you text at left and check box at right.

like image 134
Jay Rathod RJ Avatar answered Dec 06 '25 11:12

Jay Rathod RJ


Attribute android:gravity is working on view itself, while android:layout_gravity is suggestion for parent view to align it's child.

if you want tell parent layout to align it's child at left or right you have two options:

  1. use android:gravity on parent, and thus parent will try to align all child views at that gravity.
  2. android:layout_gravity on child view, and only this view will try to align itself in that gravity.
like image 38
Ioane Sharvadze Avatar answered Dec 06 '25 13:12

Ioane Sharvadze



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!