Elements overlapping on top of each other in relativeLayout

android, android-layout, android-relativelayout, textview

Solution

The solution I have found out is: Use `android:layout_below` instead of `android:layout_alignTop` / `android:layout_alignRight`

<TextView
    android:id="@+id/heading"
    android:text="My First Activity with Relative Layout"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    />

<Button
    android:id="@+id/button"
    android:text="Save"
    android:layout_width="300px"
    android:layout_height="wrap_content"
    android:layout_below="@id/heading"
    android:layout_margin="20px"
    android:layout_centerHorizontal="true"
    />

Here is the screenshot:

Problem

I have created an Activity and used `RelativeLayout` which comes in by default in Android Studio. I am trying to place a button just before my `TextView` but I am unable to do that. Here is the result: Here is my code: ``` <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="fill_parent" android:layout_height="fill_parent" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MyActivity"> <TextView android:id="@+id/heading" android:text="@string/hello_world" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <Button android:id="@+id/button" android:text="@string/sachin_jain" android:layout_width="wrap_content" android:layout_height="20dp" android:layout_alignLeft="@+id/heading" android:layout_alignTop="@+id/heading" android:layout_centerHorizontal="true" /> </RelativeLayout> ``` Looks like I am missing something. Any help would be appreciated!!

Original source

Related problems