Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Elements overlapping on top of each other in relativeLayout

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:

enter image description here

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!!

like image 874
Sachin Avatar asked Jul 15 '14 01:07

Sachin


2 Answers

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:

Snapshot with working Relative Layout

like image 196
Sachin Avatar answered Sep 28 '22 19:09

Sachin


I guess you want a button first and a textview second. Then you can do something like this:

<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_alignRight="@+id/heading"
    android:layout_alignTop="@+id/heading"
    android:layout_centerHorizontal="true"
    />

</RelativeLayout>

Your problem was that you had:

 android:layout_alignLeft="@+id/heading"
    android:layout_alignTop="@+id/heading"

in your button that made both the view align top of eachother.

like image 36
Illegal Argument Avatar answered Sep 28 '22 19:09

Illegal Argument