Question

I'm very new to Android and I have the following layout xml code:

<?xml version="1.0" encoding="utf-8"?>
<TableLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:stretchColumns="1"
android:background="#CCC">

    <TableRow
    android:background="#333"
    android:padding="10px">
     <Button 
     android:text="Sign-up"
     android:id="@+id/signUp"
     android:layout_width="wrap_content"
     android:textSize="18px"
     android:layout_height="wrap_content"
     android:layout_gravity="left">
     </Button>

     <Button 
     android:text="About"
     android:id="@+id/about"
     android:layout_width="wrap_content"
     android:textSize="18px"
     android:layout_height="wrap_content"
     android:layout_gravity="right"
     android:gravity="">
     </Button>

    </TableRow>
</TableLayout>

This produces the following layout:

alt text

How can I nudge the rightmost button to the left?

Was it helpful?

Solution

Instead of a TableLayout, try using a RelativeLayout:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="#CCC">

    <Button 
        android:text="Sign-up"
        android:id="@+id/signUp"
        android:textSize="18px"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"/>

    <Button 
        android:text="About"
        android:id="@+id/about"
        android:textSize="18px"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"/>

</RelativeLayout>

OTHER TIPS

Try setting padding on the button:

<TableRow
android:background="#333"
android:padding="10px">
 <Button 
 android:text="Sign-up"
 android:id="@+id/signUp"
 android:layout_width="wrap_content"
 android:textSize="18px"
 android:layout_height="wrap_content"
 android:layout_gravity="left">
 </Button>

 <Button 
 android:text="About"
 android:id="@+id/about"
 android:layout_width="wrap_content"
 android:textSize="18px"
 android:layout_height="wrap_content"
 android:layout_gravity="right"
 android:gravity=""
 android:padding="3dip" >
 </Button>

</TableRow>

Take a look at this tutorial, it does something very similar to what you're looking for.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top