SIGN UP MEMBER LOGIN:    
ARTICLE

RelativeLayout in Android Using Visual Studio 2010

Posted by Manish Tewatia Articles | Android Programming July 07, 2011
The RelativeLayout organizes layout components in relation to each other. This provides more flexibility of component positioning than LinearLayouts. The position of a view can be specified relative to sibling elements or in positions relative to the RelativeLayout area.
Reader Level:

Here I am discussiing the second type of view; for the first see my previous article.

RelativeLayout

The RelativeLayout organizes layout components in relation to each other. This provides more flexibility of component positioning than LinearLayouts. The position of a view can be specified relative to sibling elements or in positions relative to the RelativeLayout area; while working with this layout we need to have a very clear idea of how the elements of the screen are going to be displayed.

The drawback of this layout is that you cannot have a circular dependency between the size of the RelativeLayout and the position of its children. For example, you cannot have a RelativeLayout whose height is set to WRAP_CONTENT and a child set to ALIGN_PARENT_BOTTOM.

The relative layout widgets can be placed in one of two ways; these are:

  1. Relative to the container.

  2. Relative to other widgets.

Relative to the container: When we talk about container relative layout it means the widgets are placed in positions relative to their container. We can place the widgets in seven different positions by using these following seven properties:

  1. android:layout_alignParentTop

  2. android:layout_alignParentLeft

  3. android:layout_centerVertical

  4. android:layout_centerHorizontal

  5. android:layout_centerInParent

  6. android:layout_alignParentRight

  7. android:layout_alignParentBottom

Let's see the example one-by-one:

android:layout_alignParentTop

<TextView
     
android:layout_alignParentTop="true"
     
android:text=    "layout align Top"
     
android:textSize="30px"
     
android:background=    "#c0c0c0ff"
     
android:layout_width=    "wrap_content"
     
android:layout_height=    "wrap_content"/>

Output:

layout alignParentTop

android:layout_alignParentLeft

<TextView
     
android:layout_alignParentLeft="true"
     
android:text=    "layout align Left"
     
android:textSize="30px"
     
android:background=    "#c0c0c0ff"
     
android:layout_width=    "wrap_content"
     
android:layout_height=    "wrap_content"/>

Output:

layout align ParentLeft

android:layout_centerVertical

<TextView
     
android:layout_centerVertical="true"
     
android:text=    "layout center Vertical"
     
android:textSize="30px"
     
android:background=    "#c0c0c0ff"
     
android:layout_width=    "wrap_content"
     
android:layout_height=    "wrap_content"/>

Output:

layout align centerVertical


android:layout_centerHorizontal

<TextView
     
android:layout_centerHorizontal="true"
     
android:text=    "layout center Horizontal"
     
android:textSize="30px"
     
android:background=    "#c0c0c0ff"
     
android:layout_width=    "wrap_content"
     
android:layout_height=    "wrap_content"/>

Output:

layout align centerHorizontal

android:layout_centerInParent

<
TextView
     
android:layout_centerInParent="true"
     
android:text=    "layout aligh center In Parent"
     
android:textSize="30px"
     
android:background=    "#c0c0c0ff"
     
android:layout_width=    "wrap_content"
     
android:layout_height=    "wrap_content"/>

Output:

layout align centerInParent

android:layout_alignParentRight

<TextView      android:layout_alignParentRight="true"
     
android:text=    "layout align Right"
     
android:textSize="30px"
     
android:background=    "#c0c0c0ff"
     
android:layout_width=    "wrap_content"
     
android:layout_height=    "wrap_content"/>

Output:

layout align ParentRight

android:layout_alignParentBottom

<TextView
     
android:layout_alignParentBottom="true"
     
android:text=    "layout align Bottom"
     
android:textSize="30px"
     
android:background=    "#c0c0c0ff"
     
android:layout_width=    "wrap_content"
     
android:layout_height=    "wrap_content"/>

Output:

layout align ParentBottom

Relative to other widgets: There are four properties that determine the position of the widget in relation to other widgets:

  1. android:layout_above

  2. android:layout_below

  3. android:layout_toRightOf

  4. android:layout_toLeftOf

Example:

<?xml version="1.0" encoding="utf-8"?>
<
RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   
android:layout_width="fill_parent"
   
android:orientation="vertical"
   
android:layout_height="fill_parent">
  <
TextView
     
android:id="@+id/text1"
     
android:text=    "First"
     
android:textSize="30px"
     
android:background=    "#c0c0c0ff"
     
android:layout_width=    "wrap_content"
     
android:layout_height=    "wrap_content"/>
  <
TextView
     
android:id="@+id/text2"
     
android:text=    "second"
     
android:textSize="30px"
     
android:background=    "#ffffffff"
     
android:layout_width=    "wrap_content"
     
android:layout_height=    "wrap_content"
    
android:layout_below="@id/text1"/>
  <
TextView
     
android:id="@+id/text3"
     
android:text=    "Third"
     
android:textSize="30px"
     
android:background=    "#ff0fffff"
     
android:layout_width=    "wrap_content"
     
android:layout_height=    "wrap_content"
     
android:layout_toRightOf="@id/text2"/>
</
RelativeLayout>

Output:

Position align

Have a nice learning.....

Login to add your contents and source code to this article
share this article :
post comment
 

Jaganathan Read this article http://www.c-sharpcorner.com/uploadfile/manish1231/7506/ Complete solution....

Posted by Manish Tewatia Jul 13, 2011

Then how are you getting output? meaning, you cant create a simulator image in demo version i guess... right ?.. If not, explain how you are getting output ?

Posted by Jaganathan Bantheswaran Jul 11, 2011

No Jaganathan..........

Posted by Manish Tewatia Jul 10, 2011

Is Your Mono Android Licensed version ?

Posted by Jaganathan Bantheswaran Jul 08, 2011

You need to download Mono for Android and than you can develop Android application in visual studio 2010 using C# language..

Posted by Manish Tewatia Jul 08, 2011
Team Foundation Server Hosting
Become a Sponsor
PREMIUM SPONSORS
  • ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
    Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Nevron Gauge for SharePoint
Become a Sponsor