Francisco Romero
Francisco Romero

Reputation: 13189

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!

Upvotes: 3

Views: 8527

Answers (5)

MohamedHarmoush
MohamedHarmoush

Reputation: 1352

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.

Upvotes: 0

Jay Rathod
Jay Rathod

Reputation: 11245

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.

Upvotes: 8

Ioane Sharvadze
Ioane Sharvadze

Reputation: 2148

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.

Upvotes: 2

faranjit
faranjit

Reputation: 1627

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>

Upvotes: 0

MathankumarK
MathankumarK

Reputation: 2905

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>

Upvotes: 0

Related Questions