Android
Relative Layout In Android With Example
Last Updated on : 16th Mar 2023 20:13:58 PM
A RelativeLayout
is a very powerful Layout which is used for custum layout designing of user interface because It gives us the flexibility to position of each view can be specified as relative to sibling elements (such as to the left-of or below another view) or in positions relative to the parent RelativeLayout
area (such as aligned to the bottom, left or center).
Just because it allows us to arrange the position of UI element anywhere we want so it is considered as most flexible layout. For the same good features Relative layout is the most used layout after the Linear Layout in Android.
Positioning UI element /views in Relative Layout
In RelativeLayout we need to specify the position of child views relative to each other or relative to the parent. In case if we didn’t specify the position of child views, by default all child views are positioned to top-left of the layout.
bellow are the some of most useful layout properties available to views in RelativeLayout.
Attribute | Description |
---|---|
layout_above | It accepts another sibling view id and places the view above the specified view id. |
layout_below | It accepts another sibling view id and places the view below the specified view id. |
layout_alignParentLeft | If it specified “true”, the left edge of view will match the left edge of parent. |
layout_alignParentRight | If it specified “true”, the right edge of view will match the right edge of the parent. |
layout_centerInParent | If it specified “true”, the view will be aligned to the centre of parent. |
layout_centerHorizontal | If it specified “true”, the view will be horizontally centre aligned within its parent. |
layout_centerVertical | If it specified “true”, the view will be vertically centre aligned within its parent. |
layout_alignParentTop | If it specified “true”, the top edge of view will match the top edge of the parent. |
layout_alignParentBottom | If it specified “true”, the bottom edge of view will match the bottom edge of parent. |
layout_toLeftOf | It accepts another sibling view id and places the view left of the specified view id. |
layout_toRightOf | It accepts another sibling view id and places the view right of the specified view id. |
layout_toStartOf | It accepts another sibling view id and places the view to start of the specified view id. |
layout_toEndOf | It accepts another sibling view id and places the view to the end of the specified view id. |
Android Relative Layout Example
1. above : This is used to position the bottom edge of the given component above the given component ID.
For example, suppose a view with id textview2 is what we want to place above another view with id textview. Below is the code and layout image.
<!-- alignParentTop example -->
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Text1 align parent top"
android:layout_alignParentTop="true"
android:layout_margin="20dp"
android:textSize="20sp"
android:textColor="#000"/>
Example of Relative layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textview3"
android:text="sign in"
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true">
</TextView>
<TextView
android:id="@+id/userName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginTop="110dp"
android:text="UserName:"
android:textColor="#000000"
android:textSize="20sp" />
<TextView
android:id="@+id/password"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/userName"
android:layout_margin="20dp"
android:text="Password:"
android:textColor="#000000"
android:textSize="20sp" />
<EditText
android:id="@+id/edt_userName"
android:layout_width="fill_parent"
android:layout_height="40dp"
android:layout_marginLeft="20dp"
android:layout_marginTop="100dp"
android:layout_toRightOf="@+id/userName"
android:hint="User Name" />
<EditText
android:layout_width="fill_parent"
android:layout_height="40dp"
android:layout_below="@+id/edt_userName"
android:layout_centerVertical="true"
android:layout_toRightOf="@+id/password"
android:hint="Password" />
<Button
android:id="@+id/btnLogin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/password"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
app:backgroundTint="#93180F"
android:text="Login"
android:textColor="#ffffff"
android:textStyle="bold" />
</RelativeLayout>
ghjkl;lkjhg
<?php
$url="localhost";
$username="searchke_webs";
$password ="webservice@123";
$dbname="searchke_webs";
$connect = mysqli_connect($url,$username,$password,$dbname);
if(!$connect)
{
echo "db not connet".mysqli_connect_error();
}
else{
//echo"DB CONNECT";
}
?>
<?php
require "connect.php";
$name=$_POST["name"];
$mobile=$_POST["mobile"];
$insert="insert into register values ('$name','$mobile')";
$insert_check=mysqli_query($connect,$insert);
if(!$insert_check)
{
echo"insert not done".mysqli_error();
}
else
{
echo"insert done";
}
?>