zapotec
zapotec

Reputation: 2648

RelativeLayout not working properly Android

I have the next problem: I use a lot the RelativeLayout, like the Android documentation is telling me to do. But, since few days, I don´t understand why, this is not working anymore:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:background="@drawable/background"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <Button
        android:id="@+id/boton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="Button" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/boton"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="60dp"
        android:text="Large Text"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textColor="#000" />

</RelativeLayout>

This is my layout, very simple. So, according to this one, the textView should be BELOW the button, but, instead, it appears OVER the button!

This is a picture of the result.

Сan anyone explain me what happens?

EDIT: The problem is not when I try to run it on a device, the problem comes when I see it on the graphical layout of the ADT Plugin for Eclipse...

Upvotes: 1

Views: 3510

Answers (6)

Biplab
Biplab

Reputation: 564

try by removing the below line from your code android:layout_marginTop="60dp"

Upvotes: 1

zapotec
zapotec

Reputation: 2648

I am trying to run it, and now, I know what´s the problem.

When running the app in a terminal, it is working properly, the problems comes when I try to see it on the graphical layout on the ADT plugin on Eclipse! Anyone with the same problem??

Upvotes: 0

Arvind Kanjariya
Arvind Kanjariya

Reputation: 2097

I try this code it's working properly.

If your side not working properly Try to Clean build your project and then Run.

If still having problem then Try this

android:layout_below="@+id/boton"

remove above line with this

android:layout_below="@id/boton"

Because "+" create new reference to R.java file

This may be the issue.

Upvotes: 2

jnthnjns
jnthnjns

Reputation: 8925

I think your issue is the drawable for the background, since I am not seeing an image in your preview.

Having this issue is giving you a build error so the preview doesn't show the layout in its current state (it reverts to the last sucessful build). Either remove that line and save to see the preview or check to make sure that you have an image in your drawables folder titled "background"

Try this, without the drawable to check if it is working for you:

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

    <Button
        android:id="@+id/boton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="Button" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/boton"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="60dp"
        android:text="Large Text"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textColor="#000" />

</RelativeLayout>

Upvotes: 0

toadzky
toadzky

Reputation: 3846

Take the '+' sign out of the TextView's layout_below attribute. The '+' tells it to deine a new id. Without the '+' it should refer to the existing one.

Upvotes: 0

Luis
Luis

Reputation: 12058

Add </RelativeLayout> at the end of your relative layout to close it.

Upvotes: -1

Related Questions