{"id":292,"date":"2015-12-27T13:40:02","date_gmt":"2015-12-27T13:40:02","guid":{"rendered":"http:\/\/abhiandroid.com\/ui\/?page_id=292"},"modified":"2018-06-05T10:20:51","modified_gmt":"2018-06-05T10:20:51","slug":"relative-layout","status":"publish","type":"page","link":"https:\/\/abhiandroid.com\/ui\/relative-layout","title":{"rendered":"Relative Layout In Android With Example"},"content":{"rendered":"<p>The Relative Layout is very flexible layout used in android for custom layout designing. It gives us the flexibility to position our component\/view based on the relative or sibling component\u2019s position. Just because it allows us to position the component anywhere we want so it is considered as most flexible layout. For the same reason Relative layout is the most used layout after the Linear Layout in Android. It allow its child view to position relative to each other or relative to the container or another container.<\/p>\n<p>In Relative Layout, you can use \u201cabove, below, left and right\u201d to arrange the component\u2019s position in relation to other component. <strong>For example, in the below image you can see content is placed in related to Heading.<\/strong><\/p>\n<p><center><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-297\" src=\"\/ui\/wp-content\/uploads\/2015\/12\/Relative-Layout-Example-In-Android.jpg\" alt=\"Relative Layout Example In Android\" width=\"468\" height=\"265\" srcset=\"https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2015\/12\/Relative-Layout-Example-In-Android.jpg 468w, https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2015\/12\/Relative-Layout-Example-In-Android-300x170.jpg 300w\" sizes=\"auto, (max-width: 468px) 100vw, 468px\" \/><\/center><em>Even though Android has drag and drop system to put one component in related to other inside relative layout. But actually in the background lots of XML properties are working which does this task<\/em>. So Android developer can design UI either using drag &amp; drop or using XML code. Professional developer uses both for designing UI.<\/p>\n<hr \/>\n<h4><strong>Attributes of Relative layout:<\/strong><\/h4>\n<p>Lets see different properties of Relative Layout which will be used while designing Android App UI:<\/p>\n<p><span style=\"text-decoration: underline; color: #008000;\"><strong>1.above:<\/strong><\/span> Position the bottom edge of the view above the given anchor view ID and must be a reference of the another resource in the form of id. Example, android:layout_above=&#8221;@+id\/textView&#8221; .<\/p>\n<p>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.<\/p>\n<pre><strong>&lt;!-- textView2 is placed above textView--&gt;<\/strong>\r\n&lt;TextView\r\n        android:layout_width=\"wrap_content\"\r\n        android:layout_height=\"wrap_content\"\r\n        android:textAppearance=\"?android:attr\/textAppearanceLarge\"\r\n        android:text=\"Text2\"\r\n        android:id=\"@+id\/textView2\"\r\n        android:layout_above=\"@+id\/textView\"\r\n        android:layout_marginBottom=\"100dp\"\r\n        android:layout_centerHorizontal=\"true\"\/&gt;\r\n<\/pre>\n<p><center><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-300\" src=\"\/ui\/wp-content\/uploads\/2015\/12\/above-attribute-in-Android.jpg\" alt=\"above attribute in Android\" width=\"334\" height=\"284\" srcset=\"https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2015\/12\/above-attribute-in-Android.jpg 334w, https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2015\/12\/above-attribute-in-Android-300x255.jpg 300w\" sizes=\"auto, (max-width: 334px) 100vw, 334px\" \/><\/center><span style=\"text-decoration: underline; color: #008000;\"><strong>2. alignBottom:<\/strong><\/span> alignBottom is used to makes the bottom edge of the view match the bottom edge of the given anchor view ID and it must be a reference to another resource, in the form of id. Example: android:layout_ alignBottom =&#8221;@+id\/button1&#8243;<\/p>\n<p>In the below example we have aligned a view with id textView2 Bottom of another view with id textView. Below is the coded and layout image.<\/p>\n<pre><strong>&lt;!-- textView2 alignBottom of textView --&gt;<\/strong>\r\n&lt;TextView\r\n    android:layout_width=\"wrap_content\"\r\n    android:layout_height=\"wrap_content\"\r\n    android:textAppearance=\"?android:attr\/textAppearanceLarge\"\r\n\u00a0\u00a0\u00a0 android:layout_centerHorizontal=\"true\"    \r\n    android:id=\"@+id\/textView2\"\r\n    android:layout_alignBottom=\"@+id\/textView\"\r\n    android:text=\"Text2 alignBottom of Text1\"\r\n    android:layout_marginBottom=\"90dp\"\r\n\/&gt;<\/pre>\n<p><center><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-302 aligncenter\" src=\"\/ui\/wp-content\/uploads\/2015\/12\/alignBottom-in-Android-using-TextView.jpg\" alt=\"alignBottom in Android using TextView\" width=\"305\" height=\"245\" srcset=\"https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2015\/12\/alignBottom-in-Android-using-TextView.jpg 305w, https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2015\/12\/alignBottom-in-Android-using-TextView-300x241.jpg 300w\" sizes=\"auto, (max-width: 305px) 100vw, 305px\" \/><\/center><span style=\"text-decoration: underline; color: #008000;\"><strong>3. alignLeft:<\/strong><\/span> alignLeft is used to make the left edge of the view match the left edge of the given anchor view ID and must be a reference to another resource, in the form of Example: android:layout_ alignLeft =&#8221;@+id\/button1&#8243;.<\/p>\n<p>Below is the code and layout image in which we have aligned a view with id textView2 left of another view with id textView.<\/p>\n<pre><strong>&lt;!-- textView2 alignLeft of textView --&gt;<\/strong>\r\n&lt;TextView\r\n    android:layout_width=\"wrap_content\"\r\n    android:layout_height=\"wrap_content\"\r\n    android:textAppearance=\"?android:attr\/textAppearanceLarge\"\r\n    android:id=\"@+id\/textView2\"\r\n    android:layout_alignLeft=\"@+id\/textView\"\r\n    android:text=\"Text2 alignLeft of Text1\"\r\n    android:layout_below=\"@+id\/textView\" \r\n    android:layout_marginTop=\"20dp\"\/&gt;<\/pre>\n<p><center><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-305\" src=\"\/ui\/wp-content\/uploads\/2015\/12\/alignLeft-in-Android-Relative-Layout-Example.jpg\" alt=\"alignLeft in Android Relative Layout Example\" width=\"270\" height=\"169\" \/><\/center><span style=\"text-decoration: underline; color: #008000;\"><strong>4. alignRight:<\/strong><\/span> alignRight property is used to make the right edge of this view match the right edge of the given anchor view ID and must be a reference to another resource, in the form like this example: android:layout_alignRight=&#8221;@+id\/button1&#8243;<\/p>\n<p>Below is the code and layout image in which we have aligned a view with id textView2 right of another view with id textView.<\/p>\n<pre><strong>&lt;!-- textView2 alignRight of textView--&gt;<\/strong>\r\n&lt;TextView\r\n    android:layout_width=\"wrap_content\"\r\n    android:layout_height=\"wrap_content\"\r\n    android:textAppearance=\"?android:attr\/textAppearanceLarge\"\r\n    android:id=\"@+id\/textView2\"\r\n    android:layout_alignRight=\"@+id\/textView\"\r\n    android:text=\"Text2 alignRight of Text1\"\r\n    android:layout_below=\"@+id\/textView\"\r\n    android:layout_marginTop=\"20dp\"\/&gt;<\/pre>\n<p><center><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-306 aligncenter\" src=\"\/ui\/wp-content\/uploads\/2015\/12\/alignRight-in-Android-Relative-Layout-Example.jpg\" alt=\"alignRight in Android Relative Layout Example\" width=\"281\" height=\"169\" \/><\/center><span style=\"text-decoration: underline; color: #008000;\"><strong>5.alignStart:<\/strong><\/span> alignStart property is used to makes the start edge of this view match the start edge of the given anchor view ID and must be a reference to another resource, in the form of like this example: android:layout_alignStart=&#8221;@+id\/button1&#8243;<\/p>\n<p>Below is the alignStart code and layout image in which we have aligned a view with id textView2 start of another view with id textView.<\/p>\n<pre><strong>&lt;!-- Text2 alignStart--&gt;<\/strong>\r\n&lt;TextView\r\n    android:layout_width=\"wrap_content\"\r\n    android:layout_height=\"wrap_content\"\r\n    android:textAppearance=\"?android:attr\/textAppearanceLarge\"\r\n    android:id=\"@+id\/textView2\"\r\n    android:text=\"Text2 align start of Text1\"\r\n    android:layout_alignStart=\"@+id\/textView\"\r\n \/&gt;<\/pre>\n<p><center><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-308\" src=\"\/ui\/wp-content\/uploads\/2015\/12\/alignStart-in-Relative-Layout-Android-Example.jpg\" alt=\"alignStart in Relative Layout Android Example\" width=\"314\" height=\"165\" srcset=\"https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2015\/12\/alignStart-in-Relative-Layout-Android-Example.jpg 314w, https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2015\/12\/alignStart-in-Relative-Layout-Android-Example-300x158.jpg 300w\" sizes=\"auto, (max-width: 314px) 100vw, 314px\" \/><\/center><span style=\"text-decoration: underline; color: #008000;\"><strong>6. alignTop:<\/strong><\/span> alignTop property is used to makes the top edge of this view match the top edge of the given anchor view ID and must be a reference to another resource, in the form like this example: android:layout_alignTop=&#8221;@+id\/button1&#8243;.<\/p>\n<p>Below is the alignTop code and layout image in which we have aligned a view with id textView Top of another image with id imageView.<\/p>\n<pre><strong>&lt;!--text is align top on Image--&gt;<\/strong>\r\n&lt;TextView\r\n    android:layout_width=\"wrap_content\"\r\n    android:layout_height=\"wrap_content\"\r\n    android:textAppearance=\"?android:attr\/textAppearanceLarge\"\r\n    android:id=\"@+id\/textView\"\r\n    android:layout_alignTop=\"@+id\/imageView\"\r\n    android:text=\"Text Here is AlignTop on Image\"\r\n     \/&gt;<\/pre>\n<p><center><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-310\" src=\"\/ui\/wp-content\/uploads\/2015\/12\/alignTop-in-Android-Relative-Layout.jpg\" alt=\"alignTop in Android Relative Layout\" width=\"280\" height=\"253\" \/><\/center><span style=\"color: #008000;\"><strong>7.alignParentBottom:<\/strong> <\/span>If alignParentBottom property is true, makes the bottom edge of this view match the bottom edge of the parent. The value of align parent bottom is either true or false. Example: android:layout_alignParentBottom=&#8221;true&#8221;<\/p>\n<p><strong><span style=\"color: #ff0000; text-decoration: underline;\">Important Note:<\/span><\/strong>alignParentBottom and alignBottom are two different properties. In alignBottom we give the reference of another view in the form of id that the view is aligned at the bottom of referenced view but in alignParentBottom the bottom edge of the view matches the bottom edge of the parent.<\/p>\n<p>Below is the alignParentBottom code and layout image in which textView is simply displayed using the alignParentBottom.<\/p>\n<pre><strong>&lt;!-- textView is alignParentBottom --&gt;<\/strong>\r\n&lt;TextView\r\n    android:layout_width=\"wrap_content\"\r\n    android:layout_height=\"wrap_content\"\r\n    android:textAppearance=\"?android:attr\/textAppearanceLarge\"\r\n    android:id=\"@+id\/textView\"\r\n    android:text=\"Text Here is AlignParentBottom with bottom margin of 120dp\"\r\n    android:layout_alignParentBottom=\"true\"\r\n    android:layout_alignParentLeft=\"true\"\r\n    android:layout_alignParentStart=\"true\"\r\n    android:layout_marginBottom=\"120dp\" \/&gt;<\/pre>\n<p><center><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-312 aligncenter\" src=\"\/ui\/wp-content\/uploads\/2015\/12\/alignParentBottom-in-Android-Relative-Layout.jpg\" alt=\"alignParentBottom in Android Relative Layout\" width=\"315\" height=\"159\" srcset=\"https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2015\/12\/alignParentBottom-in-Android-Relative-Layout.jpg 315w, https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2015\/12\/alignParentBottom-in-Android-Relative-Layout-300x151.jpg 300w\" sizes=\"auto, (max-width: 315px) 100vw, 315px\" \/><\/center><span style=\"text-decoration: underline; color: #008000;\"><strong>8. alignParentEnd:<\/strong><\/span> If alignParentEnd property is true, then it makes the end edge of this view match the end edge of the parent. The value of align parent End\u00a0is either true or false. Example: android:layout_alignParentEnd=&#8221;true&#8221;.<\/p>\n<p><span style=\"color: #ff0000;\"><strong>Important Note: <\/strong><\/span>In\u00a0alignParentEnd the bottom edge of the view matches the bottom edge of the parent.<\/p>\n<p>Below is the alignParentEnd code and layout image in which textView is simply displayed on Image in the end.<\/p>\n<pre><strong>&lt;!-- Text displayed in the end of parent image--&gt;<\/strong>\r\n&lt;TextView\r\n    android:layout_width=\"wrap_content\"\r\n    android:layout_height=\"wrap_content\"\r\n    android:textAppearance=\"?android:attr\/textAppearanceLarge\"\r\n    android:id=\"@+id\/textView\"\r\n    android:text=\"Text in Parent End\"\r\n    android:layout_alignBottom=\"@+id\/imageView\"\r\n    android:layout_alignParentEnd=\"true\"\r\n \/&gt;<\/pre>\n<p><center><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-315 aligncenter\" src=\"\/ui\/wp-content\/uploads\/2015\/12\/alignParentEnd-in-Relative-layout.jpg\" alt=\"alignParentEnd in Relative layout\" width=\"309\" height=\"257\" srcset=\"https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2015\/12\/alignParentEnd-in-Relative-layout.jpg 309w, https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2015\/12\/alignParentEnd-in-Relative-layout-300x250.jpg 300w\" sizes=\"auto, (max-width: 309px) 100vw, 309px\" \/><\/center><span style=\"text-decoration: underline; color: #008000;\"><strong>9. alignParentLeft:<\/strong><\/span> If alignParentLeft property is true, makes the left edge of this view match the left edge of the parent. The value of align parent left\u00a0is either true or false. Example: android:layout_alignParentLeft=&#8221;true&#8221;.<\/p>\n<p><strong><span style=\"color: #ff0000;\">Important Note:<\/span>\u00a0<\/strong>alignParentLeft and alignLeft are two different properties. In alignLeft we give the reference of another view in the form of id that the view is aligned to the left\u00a0of the referenced view but in alignParentLeft the left\u00a0edge of the view matches the left\u00a0edge of the parent.<\/p>\n<p>Below is the alignParentLeft example code and layout image in which textView is simply displayed on parent Image in the left side.<\/p>\n<pre><strong>&lt;!-- align parent left in Android --&gt;<\/strong>\r\n&lt;TextView\r\n    android:layout_width=\"wrap_content\"\r\n    android:layout_height=\"wrap_content\"\r\n    android:textAppearance=\"?android:attr\/textAppearanceLarge\"\r\n    android:id=\"@+id\/textView\"\r\n    android:text=\"Text in Parent Left\" \r\n    android:layout_alignBottom=\"@+id\/imageView\"\r\n    android:layout_alignParentLeft=\"true\"\r\n     \/&gt;<\/pre>\n<p><center><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-316\" src=\"\/ui\/wp-content\/uploads\/2015\/12\/alignParentLeft-in-Android-Relative-Layout.jpg\" alt=\"alignParentLeft in Android Relative Layout\" width=\"248\" height=\"248\" srcset=\"https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2015\/12\/alignParentLeft-in-Android-Relative-Layout.jpg 248w, https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2015\/12\/alignParentLeft-in-Android-Relative-Layout-150x150.jpg 150w\" sizes=\"auto, (max-width: 248px) 100vw, 248px\" \/><\/center><span style=\"text-decoration: underline; color: #008000;\"><strong>10. alignParentRight:<\/strong><\/span> If alignParentRight property is true, then it makes the right edge of this view match the right edge of the parent. The value of align parent right\u00a0is either true or false. Example: android:layout_alignParentRight=&#8221;true&#8221;.<\/p>\n<p><strong><span style=\"color: #ff0000;\">Important Note:<\/span>\u00a0<\/strong>alignParentRight\u00a0and alignRight are two different properties. In alignRight we give the reference of another view in the form of id that the view is aligned to the right\u00a0of the referenced view but in alignParentRight\u00a0the right\u00a0edge of the view matches the right\u00a0edge of the parent.<\/p>\n<p>Below is the alignParentRight example code and layout image in which textView is simply displayed on parent Image in the right side.<\/p>\n<pre><strong>&lt;!-- alignRightParent Example --&gt;<\/strong>\r\n&lt;TextView\r\n    android:layout_width=\"wrap_content\"\r\n    android:layout_height=\"wrap_content\"\r\n    android:textAppearance=\"?android:attr\/textAppearanceLarge\"\r\n    android:id=\"@+id\/textView\"\r\n    android:text=\"Text in Parent Right\"\r\n    android:layout_alignBottom=\"@+id\/imageView\"\r\n    android:layout_alignParentRight=\"true\"\r\n  \/&gt;<\/pre>\n<p><center><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-317\" src=\"\/ui\/wp-content\/uploads\/2015\/12\/alignParentRight-in-Relative-Layout-Android.jpg\" alt=\"alignParentRight in Relative Layout Android\" width=\"233\" height=\"247\" \/><\/center><span style=\"text-decoration: underline; color: #008000;\"><strong>11.alignParentStart:<\/strong><\/span> If alignParentStart is true, then it makes the start edge of this view match the start edge of the parent. The value of align parent start\u00a0is either true or false. Example: android:layout_alignParentStart=&#8221;true&#8221;.<\/p>\n<p><strong><span style=\"color: #ff0000;\">Important Note:<\/span>\u00a0<\/strong>alignParentStart and alignStart are two different properties, In alignStart we give the reference of another view in the form of id that the view is aligned at the start of referenced view but in alignParentStart the start\u00a0edge of the view matches the start\u00a0edge of the parent(RelativeLayout).<\/p>\n<p>Below is the alignParentStart example code and layout image in which textView is simply displayed on parent Image in the right side.<\/p>\n<pre><strong>&lt;!-- alignParentStart Example --&gt;<\/strong>\r\n&lt;TextView\r\n    android:layout_width=\"wrap_content\"\r\n    android:layout_height=\"wrap_content\"\r\n    android:textAppearance=\"?android:attr\/textAppearanceLarge\"\r\n    android:id=\"@+id\/textView\"\r\n    android:text=\"Text in Parent Start\"\r\n    android:layout_alignTop=\"@+id\/imageView\"\r\n    android:layout_alignParentStart=\"true\"\r\n     \/&gt;<\/pre>\n<p><center><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-320\" src=\"\/ui\/wp-content\/uploads\/2015\/12\/alignParentStart-in-Relative-Layout.jpg\" alt=\"alignParentStart in Relative Layout\" width=\"230\" height=\"256\" \/><\/center><span style=\"text-decoration: underline; color: #008000;\"><strong>12.alignParentTop:<\/strong><\/span> If alignParentTop is true, then it makes the top edge of this view match the top edge of the parent. The value of align parent Top is either true or false. Example: android:layout_alignParenTop=&#8221;true&#8221;.<\/p>\n<p><strong><span style=\"color: #ff0000;\">Important Note:<\/span>\u00a0<\/strong>alignParentTop and alignTop are two different properties, In alignTop we give the reference of another view in the form of id that the view is aligned to the top\u00a0of the referenced view but in alignParentTop the top\u00a0edge of the view matches the top\u00a0edge of the parent(RelativeLayout).<\/p>\n<p>Below is the example code of alignParentTop property and also layout image.<\/p>\n<pre><strong>&lt;!-- alignParentTop example --&gt;<\/strong>\r\n&lt;TextView\r\n    android:layout_width=\"wrap_content\"\r\n    android:layout_height=\"wrap_content\"\r\n    android:text=\"Text1 align parent top\"\r\n    android:layout_alignParentTop=\"true\"\r\n    android:layout_margin=\"20dp\"\r\n    android:textSize=\"20sp\"\r\n    android:textColor=\"#000\"\/&gt;<\/pre>\n<p><center><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-334\" src=\"\/ui\/wp-content\/uploads\/2015\/12\/alignParentTop-in-Android-Relative-Layout.jpg\" alt=\"alignParentTop in Android Relative Layout\" width=\"218\" height=\"164\" \/><\/center><span style=\"color: #008000;\"><strong>13.centerInParent:<\/strong><\/span> If center in parent is true, makes the view in the center of the screen vertically and horizontally. The value of center in parent\u00a0is either true or false. Example: android:layout_centerInParent=&#8221;true&#8221;.<\/p>\n<p>Below is the example code of centerInParent property and also layout image.<\/p>\n<pre><strong>&lt;!-- centerInParent example --&gt;<\/strong>\r\n&lt;TextView\r\n    android:layout_width=\"wrap_content\"\r\n    android:layout_height=\"wrap_content\"\r\n    android:text=\"Text1 center in parent\"\r\n    android:layout_centerInParent=\"true\"\r\n    android:textSize=\"20sp\"\r\n    android:textColor=\"#000\"\r\n\/&gt;<\/pre>\n<p><center><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-335\" src=\"\/ui\/wp-content\/uploads\/2015\/12\/center-in-parent-Android-Relative-Layout.jpg\" alt=\"center in parent Android Relative Layout\" width=\"220\" height=\"355\" srcset=\"https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2015\/12\/center-in-parent-Android-Relative-Layout.jpg 220w, https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2015\/12\/center-in-parent-Android-Relative-Layout-186x300.jpg 186w\" sizes=\"auto, (max-width: 220px) 100vw, 220px\" \/><\/center><span style=\"color: #008000;\"><strong>14.centerHorizontal:<\/strong><\/span> If centerHorizontal property is true, makes the view horizontally center. The value of centerHorizontal\u00a0is either true or false.Example: android:layout_centerHorizontal=&#8221;true&#8221;.<\/p>\n<p>Below is the example code of centerHorizontal property and also layout image.<\/p>\n<pre><strong>&lt;!-- centerHorizontal example --&gt;<\/strong>\r\n&lt;TextView\r\n    android:layout_width=\"wrap_content\"\r\n    android:layout_height=\"wrap_content\"\r\n    android:text=\"Text1 center Horizontal\"\r\n    android:layout_centerHorizontal=\"true\"\r\n    android:textSize=\"20sp\"\r\n    android:textColor=\"#000\"\r\n\/&gt;<\/pre>\n<p><center><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-336\" src=\"\/ui\/wp-content\/uploads\/2015\/12\/center-horizontal-in-Android-Relative-Layout.jpg\" alt=\"center horizontal in Android Relative Layout\" width=\"221\" height=\"189\" \/><\/center><span style=\"color: #008000;\"><strong>15.centerVertical:<\/strong><\/span> If centerVertical property is true, make the view vertically center. The value of align parent bottom is either true or false. Example: android:layout_centerVertical=&#8221;true&#8221;.<\/p>\n<p>Below is the example code of centerVertical property and also layout image.<\/p>\n<pre><strong>&lt;!-- centerVertical example --&gt;<\/strong>\r\n&lt;TextView\r\n    android:layout_width=\"wrap_content\"\r\n    android:layout_height=\"wrap_content\"\r\n    android:text=\"Text1 center vertical\"\r\n    android:layout_centerVertical=\"true\"\r\n    android:textSize=\"20sp\"\r\n    android:textColor=\"#000\"\r\n\/&gt;<\/pre>\n<p><center><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-337\" src=\"\/ui\/wp-content\/uploads\/2015\/12\/center-vertical-in-Android-Relative-Layout.jpg\" alt=\"center vertical in Android Relative Layout\" width=\"221\" height=\"354\" srcset=\"https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2015\/12\/center-vertical-in-Android-Relative-Layout.jpg 221w, https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2015\/12\/center-vertical-in-Android-Relative-Layout-187x300.jpg 187w\" sizes=\"auto, (max-width: 221px) 100vw, 221px\" \/><\/center><\/p>\n<hr \/>\n<h4><strong>Relative Layout Examples With Code And Explanation:<\/strong><\/h4>\n<p><span style=\"color: #008000;\"><strong>Example 1:<\/strong><\/span> Here we are designing a simple log in screen in Android Studio using Relative Layout. Below is the final output:<\/p>\n<p><center><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-396\" src=\"\/ui\/wp-content\/uploads\/2015\/12\/Relative-Layout-Login-Signin-Example-Output.jpg\" alt=\"Relative Layout Login Signin Example Output\" width=\"264\" height=\"431\" srcset=\"https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2015\/12\/Relative-Layout-Login-Signin-Example-Output.jpg 264w, https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2015\/12\/Relative-Layout-Login-Signin-Example-Output-184x300.jpg 184w\" sizes=\"auto, (max-width: 264px) 100vw, 264px\" \/><\/center>Below is the code of activity_main.xml for designing UI with explanation included in it:<\/p>\n<pre>&lt;?xml version=\"1.0\" encoding=\"utf-8\"?&gt;\r\n\r\n<strong>&lt;!--Relative Layout Is Used--&gt;<\/strong>\r\n\r\n&lt;RelativeLayout xmlns:android=\"http:\/\/schemas.android.com\/apk\/res\/android\"\r\n    android:layout_width=\"match_parent\"\r\n    android:layout_height=\"match_parent\"&gt;\r\n\r\n    <strong>&lt;!--Text View for Displaying SIGN IN Text At Top of UI--&gt;<\/strong>\r\n\r\n    &lt;TextView\r\n        android:layout_width=\"wrap_content\"\r\n        android:layout_height=\"wrap_content\"\r\n        android:textAppearance=\"?android:attr\/textAppearanceLarge\"\r\n        android:text=\"SIGN IN\"\r\n        android:id=\"@+id\/textView3\"\r\n        android:layout_alignParentTop=\"true\"\r\n        android:layout_centerHorizontal=\"true\" \/&gt;\r\n\r\n    <strong>&lt;!--Text View for Displaying Username--&gt;<\/strong>\r\n\r\n    &lt;TextView\r\n        android:id=\"@+id\/userName\"\r\n        android:layout_width=\"wrap_content\"\r\n        android:layout_height=\"wrap_content\"\r\n        android:layout_marginLeft=\"@dimen\/activity_horizontal_margin\"\r\n        android:layout_marginTop=\"110dp\"\r\n        android:text=\"UserName:\"\r\n        android:textColor=\"#000000\"\r\n        android:textSize=\"20sp\" \/&gt;\r\n\r\n    <strong>&lt;!--Text View for Displaying Password--&gt;<\/strong>\r\n\r\n    &lt;TextView\r\n        android:id=\"@+id\/password\"\r\n        android:layout_width=\"wrap_content\"\r\n        android:layout_height=\"wrap_content\"\r\n        android:layout_below=\"@+id\/userName\"\r\n        android:layout_margin=\"@dimen\/activity_horizontal_margin\"\r\n        android:text=\"Password:\"\r\n        android:textColor=\"#000000\"\r\n        android:textSize=\"20sp\" \/&gt;\r\n\r\n    <strong>&lt;!--Edit Text for Filling Username--&gt;<\/strong>\r\n\r\n    &lt;EditText\r\n        android:id=\"@+id\/edt_userName\"\r\n        android:layout_width=\"fill_parent\"\r\n        android:layout_height=\"40dp\"\r\n        android:layout_marginLeft=\"@dimen\/activity_horizontal_margin\"\r\n        android:layout_marginTop=\"100dp\"\r\n        android:layout_toRightOf=\"@+id\/userName\"\r\n        android:hint=\"User Name\" \/&gt;\r\n\r\n    <strong>&lt;!--Edit Text for Filling Password--&gt;<\/strong>\r\n\r\n    &lt;EditText\r\n        android:layout_width=\"fill_parent\"\r\n        android:layout_height=\"40dp\"\r\n        android:layout_below=\"@+id\/edt_userName\"\r\n        android:layout_centerVertical=\"true\"\r\n        android:layout_toRightOf=\"@+id\/password\"\r\n        android:hint=\"Password\" \/&gt;\r\n\r\n    <strong>&lt;!--Button for Clicking after filling details--&gt;<\/strong>\r\n\r\n    &lt;Button\r\n        android:id=\"@+id\/btnLogin\"\r\n        android:layout_width=\"wrap_content\"\r\n        android:layout_height=\"wrap_content\"\r\n        android:layout_below=\"@+id\/password\"\r\n        android:layout_centerHorizontal=\"true\"\r\n        android:layout_marginTop=\"20dp\"\r\n        android:background=\"#03B424\"\r\n        android:text=\"Login\"\r\n        android:textColor=\"#ffffff\"\r\n        android:textStyle=\"bold\" \/&gt;\r\n\r\n\r\n&lt;\/RelativeLayout&gt;<\/pre>\n<p><span style=\"text-decoration: underline;\"><strong>Output:<\/strong><\/span><\/p>\n<p><center><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-396\" src=\"\/ui\/wp-content\/uploads\/2015\/12\/Relative-Layout-Login-Signin-Example-Output.jpg\" alt=\"Relative Layout Login Signin Example Output\" width=\"264\" height=\"431\" srcset=\"https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2015\/12\/Relative-Layout-Login-Signin-Example-Output.jpg 264w, https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2015\/12\/Relative-Layout-Login-Signin-Example-Output-184x300.jpg 184w\" sizes=\"auto, (max-width: 264px) 100vw, 264px\" \/><\/center><\/p>\n<hr \/>\n<h4><strong>Difference between Linear And Relative Layout:<\/strong><\/h4>\n<p><strong>RELATIVE LAYOUT:<\/strong><\/p>\n<ul>\n<li>Every element of relative layout arranges itself to the other element or a parent element.<\/li>\n<li>It is helpful while adding views one next to other etc<\/li>\n<li>In a relative layout you can give each child a Layout Property that specifies exactly where it should go in relative to the parent or relative to other children.<\/li>\n<li>Views can be layered on top of each other.<\/li>\n<\/ul>\n<p><strong>LINEAR LAYOUT:<\/strong><\/p>\n<ul>\n<li>In a linear layout, like the name suggests, all the elements are displayed in a linear fashion either vertically or horizontally.<\/li>\n<li>Either Horizontally or Vertically this behavior is set in android:orientation which is an property of the node Linear Layout.\n<pre>android:orientation=\u201dhorizontal\u201d\u00a0 or android:orientation=\u201dvertical\u201d<\/pre>\n<\/li>\n<li>Linear layouts put every child, one after the other, in a line, either horizontally or vertically.<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>The Relative Layout is very flexible layout used in android for custom layout designing. It gives us the flexibility to position our component\/view based on the relative or sibling component\u2019s position. Just because it allows us to position the component anywhere we want so it is considered as most flexible layout. For the same reason &hellip; <a href=\"https:\/\/abhiandroid.com\/ui\/relative-layout\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Relative Layout In Android With Example<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"open","ping_status":"closed","template":"home.php","meta":{"_acf_changed":false,"footnotes":""},"class_list":["post-292","page","type-page","status-publish","hentry"],"acf":[],"psp_head":"<title>Relative Layout In Android With Example \u2013 Abhi Android<\/title>\r\n<meta name=\"description\" content=\"The tutorial on relative layout discussing how views are placed in related to other with examples, code, attributes, images and screenshot. Learn to design linear Android UI.\" \/>\r\n<meta name=\"robots\" content=\"index,follow\" \/>\r\n<link rel=\"canonical\" href=\"https:\/\/abhiandroid.com\/ui\/relative-layout\" \/>\r\n","_links":{"self":[{"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/pages\/292","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/comments?post=292"}],"version-history":[{"count":2,"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/pages\/292\/revisions"}],"predecessor-version":[{"id":2650,"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/pages\/292\/revisions\/2650"}],"wp:attachment":[{"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/media?parent=292"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}