Reputation: 13189
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
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
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 andcheck box
at right.
Upvotes: 8
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:
android:gravity
on parent, and thus parent will try to align all child views at that gravity. android:layout_gravity
on child view, and only this view will try to align itself in that gravity. Upvotes: 2
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
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