views:

117

answers:

3

Hello all,

I am trying to layout a Button to the left of a TextView that is centered on the screen. My layout looks like this:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="center">

 <TextView  
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="foo"
     android:id="@+id/center"
     />

    <Button android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:text="Left button"
     android:layout_toLeftOf="@id/center" />
</RelativeLayout>

Unfortunately, the button just does not appear. I get the following result: alt text

As you see, the button doesn't show up. It works if I use layout_toRightOf, then the button appears to the right of the TextView, just as expected.

Any ideas what I am doing wrong here?

+1  A: 

Try

<Button android:id="@+id/button"
     android:layout_alignParentLeft="true"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:text="Left button">
</Button>
<TextView  
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="foo"
     android:layout_toRightOf="@id/button"
     android:id="@+id/center">
</TextView>
BrennaSoft
Rpond, thanks for your response. If I follow your suggestion, the TextView is not centered on the screen any more. Any ideas how to ensure that the TextView is centered? Thanks.
rodion
+1  A: 
  1. You have not provided any rules for positioning the TextView. If you want the TextView centered, say so (e.g., android:layout_centerInParent="true")
  2. You have not provided any rules for vertically positioning the Button.
  3. Use hierarchyviewer to inspect your layout to figure out where things are being positioned.
CommonsWare
Your post made me realize that I know hardly anything about layout on Android. Thanks for the helpful pointers.
rodion
A: 

You can also pad your text to move it where you want...

Mike Droid