Place a view above another view in relativelayout android

Set a view upper top side of another widget in RelativeLayout android using layout xml.

Placing the first view widget and adding more views after it on above location is very easy using layout xml file. So here is the complete step by step tutorial for how to Place a view above another view in relativelayout android.

android-project-download-code-button

How to Place a view above another view in relativelayout android.

Code for MainActivity.java file.

package com.placeaviewabove_android_examples.com;
import android.app.Activity;
import android.os.Bundle;

public class MainActivity extends Activity {

 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 }
}

Code for activity_main.xml layout file.

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:paddingBottom="@dimen/activity_vertical_margin"
 android:paddingLeft="@dimen/activity_horizontal_margin"
 android:paddingRight="@dimen/activity_horizontal_margin"
 android:paddingTop="@dimen/activity_vertical_margin"
 tools:context="com.placeaviewabove_android_examples.com.MainActivity" >

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

 <Button
 android:id="@+id/button2"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_above="@+id/button1"
 android:layout_centerHorizontal="true"
 android:layout_marginBottom="20dp"
 android:text="SECOND WIDGET" />

 <Button
 android:id="@+id/button3"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_alignLeft="@+id/button1"
 android:layout_below="@+id/button1"
 android:layout_marginTop="20dp"
 android:text="THIRD WIDGET" />

</RelativeLayout>

Screenshot:

Place a view above another view in relativelayout android

Click here to download Place a view above another view in RelativeLayout android project.

Leave a Reply

Your email address will not be published. Required fields are marked *