Align text left, checkbox right

I'm trying to create a layout for a ListView, with a checkbox to the right and some text to the left. The checkbox should be aligned all the way to the right and the TextView aligned all the way to left of the row, eg:

 ------------------------
 text            checkbox
 ------------------------
 text            checkbox
 ------------------------
 text            checkbox
 ------------------------

This is what I have so far:

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"
      android:orientation="vertical"
      android:paddingLeft="5dip"
      android:paddingRight="5dip"
      android:paddingTop="8dip"
     android:paddingBottom="8dip"
>

<TextView  
    android:id="@+id/text"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="left" 
/>

<CheckBox 
    android:id="@+id/chekcbox"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="right" 
 /> 

 </RelativeLayout>

However, what actually renders is the TextBox is overlaying the checkbox, to the left of the row.


Solution 1:

And to get the checkbox's box to the right , in check box attributes

android:button="@null"
android:drawableRight="?android:attr/listChoiceIndicatorMultiple"

Solution 2:

Since you are already using a RelativeLayout, make use of it's attributes:

Remove android:gravity from the children and replace them with android:layout_alignParentLeft="true" for the TextView and android:layout_alignParentRight="true" for the CheckBox.

That positions the children relative to the parents borders. You may also want to add android:layout_centerVertical="true" to each child to center the two vertical within each list item.

For further options and attributes see the documentation.

Solution 3:

for first time you must set button to @null

android:button="@null"

if you want to onl move android checkbox to right use :

android:drawableRight="?android:attr/listChoiceIndicatorMultiple"

otherwise if you like to have custom image for checkbox use:

android:drawableRight="@drawable/selector_check_box"

and for set gravity to right :

android:gravity="right|center_vertical"

full action for use customize checkbox:

<?xml version="1.0" encoding="utf-8"?>
<selector
  xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_window_focused="false" android:state_enabled="true" android:state_checked="true" android:drawable="@drawable/_ics_btn_check_on" />
    <item android:state_window_focused="false" android:state_enabled="true" android:state_checked="false" android:drawable="@drawable/_ics_btn_check_off" />
    <item android:state_enabled="true" android:state_checked="true" android:state_pressed="true" android:drawable="@drawable/_ics_btn_check_on_pressed" />
    <item android:state_enabled="true" android:state_checked="false" android:state_pressed="true" android:drawable="@drawable/_ics_btn_check_off_pressed" />
    <item android:state_enabled="true" android:state_checked="false" android:drawable="@drawable/_ics_btn_check_off" />
    <item android:state_enabled="true" android:state_checked="true" android:drawable="@drawable/_ics_btn_check_on" />
    <item android:state_window_focused="false" android:state_checked="true" android:drawable="@drawable/_ics_btn_check_on_disabled" />
    <item android:state_window_focused="false" android:state_checked="false" android:drawable="@drawable/_ics_btn_check_off_disabled" />
    <item android:state_checked="false" android:drawable="@drawable/_ics_btn_check_off_disabled" />
    <item android:state_checked="true" android:drawable="@drawable/_ics_btn_check_on_disabled" />
</selector>

images :

enter image description hereenter image description hereenter image description hereenter image description hereenter image description here

Solution 4:

My solution in this case is to use:

<CheckBox
...
    android:button="@null"
    android:drawableRight="@drawable/my_check"
...
/>

where my_radio could be for example you custom selector! Example of selector could be:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_checked="true" android:drawable="@drawable/check_checked" />
    <item android:state_checked="false" android:drawable="@drawable/check_unchecked" />
</selector>

Solution 5:

Use this as your LIST / RECYCLER ITEM. The below 2 lines are the key.

android:textDirection="ltr" android:layoutDirection="rtl"

<CheckBox
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:textDirection="ltr"
            android:layoutDirection="rtl"
            />