Relative Layout Examples


Relative Layout



Relative Layout Example




MainActivity.java

package com.zyasin.relativelayout;

import android.os.Bundle;
import android.app.Activity;

public class MainActivity extends Activity {
   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
   }
}




activity_main.xml

 <?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="fill_parent"

    android:layout_height="fill_parent" >

 
   <Button

        android:id="@+id/button1"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="Button 1">


       <Button 
         android:id="@+id/button2"

         android:layout_width="wrap_content"

         android:layout_height="wrap_content"

         android:layout_alignParentLeft="true"

         android:layout_below="@+id/button1"

         android:text="Button 2">

 

     <Button

         android:id="@+id/button3"

         android:layout_width="wrap_content"

         android:layout_height="wrap_content"

         android:layout_below="@+id/button2"

         android:layout_toRightOf="@+id/button2"

         android:text="Button 3">

 
  <RelativeLayout>



Relative Layout - Percentage Dimensions and Margins


Percentage support library provide features to set the percentage widths and margins for relative layout, using PercentRelativeLayout, with possible attributes of layout_widthPercent, layout_heightPercent,layout_marginPercent, layout_marginPercent, layout_marginPercent, layout_marginPercent, layout_marginPercent, layout_marginStartPercent and layout_marginEndPercent.


MainActivity.java

package com.zyasin.relativelayout;

import android.os.Bundle;
import android.app.Activity;

public class MainActivity extends Activity {
   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
   }
}
activity_main.xml

< android.support.percent.PercentRelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">

 
<TextView
android:id="@+id/textview_title"
android:layout_centerHorizontal="true"
app:layout_heightPercent="11%"
android:gravity="center"
app:layout_marginTopPercent="14%"
app:layout_widthPercent="100%"
android:textSize="50dp"
android:text="TEXT"/>



<Button
android:id="@+id/button_blogger"
android:layout_below="@+id/textview_sub_header"
android:text="button"
android:textColor="@android:color/white"
android:textSize="25dp"
android:textStyle="bold"
android:background="@android:color/holo_blue_light"
app:layout_heightPercent="13%"
app:layout_marginTopPercent="16%"
app:layout_widthPercent="100%"/>

 
<TextView
android:id="@+id/textview_bottom_text"
android:layout_height="match_parent"
android:layout_below="@+id/button_blogger"
android:gravity="center"
android:text="TEXT"
android:textSize="20dp"
app:layout_widthPercent="100%"/>
 
</android.support.percent.PercentRelativeLayout/>


Tweet  




Copyright (c) Zafar Yasin. All rights reserved.