Search code examples
androidlayoutalignmentandroid-datepicker

Android DatePicker right alignment in TableRow


I have added a DatePicker to a TableRow inside a TableLayout. I am trying to perform right alignment of the DatePicker (see the code below). However, when I run the application the date picker is not aligned to the right.

<TableLayout
    android:id="@+id/_layout"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" >

    <TableRow
        android:id="@+id/_row"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="right"
        android:layout_weight="1" >

        <DatePicker
            android:id="@+id/_date"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="right"
            android:layout_marginLeft="5dip"
            android:layout_marginRight="5dip"
            android:layout_weight="1" />
        </TableRow>
</TableLayout>

How can I align the date picker to the right?


Solution

  • You have to do something like this:

    <TableRow
        android:id="@+id/_row"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="right" >
    
        <DatePicker
            android:id="@+id/_date"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="right"
            android:layout_marginLeft="5dip"
            android:layout_marginRight="5dip" />
        </TableRow>
    

    I've changed only the width of the date picker to wrap_content, the date picker is to the right but because is stretched to fill the parent, you can't see that..