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!!
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:
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With