3

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!

Francisco Romero
  • 12,787
  • 22
  • 92
  • 167

5 Answers5

8

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.

Jay Rathod
  • 11,131
  • 6
  • 34
  • 58
  • It'd be helpful if you could also explain what does this code do? `android:button="@null"` – hsm59 Aug 30 '18 at 09:37
2

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.
Ioane Sharvadze
  • 2,118
  • 21
  • 35
  • 1
    Thank you very much! Your explanation is the only who works for me. An addition to your answer, I had to remove `android:layout_weight="1"`. – Francisco Romero Aug 02 '16 at 12:16
0

Try this. You will get both text and checkbox to the right side

<TableRow
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="right"
        android:layout_marginBottom="10dp"
        android:gravity="right">

        <CheckBox
            android:id="@+id/checkboxSelect"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:checked="true"
            android:gravity="right|center_vertical"
            android:text="Select" />
    </TableRow>
MathankumarK
  • 2,717
  • 1
  • 17
  • 34
0

Try with RelativeLayout

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

      <CheckBox
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_toLeftOf="@id/text_view"
            android:id="@+id/checkboxSelect"/>

      <TextView 
            android:id="@+id/text_view"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"/>
</RelativeLayout>
faranjit
  • 1,567
  • 1
  • 15
  • 22
0

Try this

    <CheckBox
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="center|end"
    android:layout_gravity="start"
    android:button="@null"
    android:drawableRight="?android:attr/listChoiceIndicatorMultiple"
    android:text="hello" />

Set android:button="@null" and set this drawable "?android:attr/listChoiceIndicatorMultiple" as drawableRight or drawableLeft as you like.

MohamedHarmoush
  • 1,033
  • 11
  • 17