3

I want to put a TextView on the left-side and a control (eg. a CheckBox) on the right of the TextView. I want the control to be left-aligned on the screen. This is not hard to obtain through a LinearLayout or a RelativeLayout. For example I did this with LinearLayout:

  <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >
    <TextView
        android:id="@+id/todo_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="@android:style/TextAppearance.Small"
         android:maxLines="2"
        android:textStyle="bold" />
    <View
        android:layout_width="0dp"
        android:layout_height="fill_parent"
        android:layout_weight="1" />
    <CheckBox
        android:id="@+id/todo_checkbox"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:enabled="false"
        android:focusable="false"/>
 </LinearLayout>

The problem is that when the TextView's text is too long, it pushes the checkbox out of the screen and the checkbox is no longer visible. Instead, I want that the checkbox is fixed at the right-end of the screen and the TextView eventually split in two lines if necessary. How can I achieve this?

user1781028
  • 1,478
  • 4
  • 22
  • 45

2 Answers2

7

Use android:layout_weight="1" for textview and checkbox will always be on right side. Check this: Android Layout Weight

<LinearLayout
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:orientation="horizontal" >
   <TextView
      android:id="@+id/todo_title"
      android:layout_width="0dp"
      android:layout_height="wrap_content"
      android:textAppearance="@android:style/TextAppearance.Small"
      android:maxLines="2"
      android:layout_weight="1"
      android:textStyle="bold" />
  <CheckBox
      android:id="@+id/todo_checkbox"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:enabled="false"
      android:focusable="false"/>
</LinearLayout>
Community
  • 1
  • 1
Koso
  • 3,200
  • 2
  • 20
  • 22
0

Use weightsum and layout_weight.

Weightsum is a value given to a parent, saying all children components have to add up to the sum.

Layout_weight is given to the children of that layout. The value corresponds to the amount of the layout the component will occupy.

<LinearLayout
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:orientation="horizontal"
   android:weightsum = "100" >
   <TextView
      android:id="@+id/todo_title"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:textAppearance="@android:style/TextAppearance.Small"
      android:maxLines="2"
      android:layout_weight="30"
      android:textStyle="bold" />
  <CheckBox
      android:id="@+id/todo_checkbox"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:enabled="false"
      android:layout_weight="70"
      android:focusable="false"/>

</LinearLayout>

Keep in mind that the values are reversed (not sure how it works), but the largest layout_weight value occupies the smallest area.

Alexey
  • 3,607
  • 8
  • 34
  • 54
  • Play around with the values of layout_weight. Does it yield the same result for all values? – Alexey Jul 12 '13 at 15:02