Saturday, 28 July 2012

Android Relative Layout Tutorial


Introduction of Android Relative Layout



Every android control is put in predefined layout. layout provides to arrange android controls. relative layout position android controls relative to another. Using this developer can display his desire UI. For defining relative position we use android:layout_below,layout_toRightOf,layout_toLeftOf etc. properties.

How to define Relative layout

Android use seperate file for layout. This file stored in android res/layout directory.

syntex :
 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
 
  <RelativeLayout/>

 
Above syntex define following properties
  • android:layout_width this property is used to define width.
  • android:layout_height this property is used to define height.
Example
  1. Make layout file(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:text="Button 2" 
            android:layout_below="@+id/btnButton1"/>
     
     </RelativeLayout>
    
  2. Output : 


            

No comments:

Post a Comment