Monday, June 18, 2012

How To Work With Relative Layout With Example.


The project describes the use of relative layout.
Underlying Algorithm:
Basic description of algorithm in step by step form:
1.) Create a Project MyFirstRelativeLayout.
2.) Open the res/layout/main.xml file and insert the following:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent">
    <TextView

       android:id="@+id/label"
       android:layout_width="fill_parent"
       android:layout_height="wrap_content"
       android:text="Type here:"/>
    <EditText
       android:id="@+id/entry"
       android:layout_width="fill_parent"
       android:layout_height="wrap_content"
       android:background="@android:drawable/editbox_background"
       android:layout_below="@id/label"/>
    <Button
       android:id="@+id/ok"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_below="@id/entry"
       android:layout_alignParentRight="true"
       android:layout_marginLeft="10dip"
       android:text="OK" />
    <Button
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_toLeftOf="@id/ok"
       android:layout_alignTop="@id/ok"
       android:text="Cancel" />
</RelativeLayout>

When using a RelativeLayout, you can use these attributes to describe how you want to position each View. Each one of these attributes defines a different kind of relative position. Some attributes use the resource ID of a sibling View to define its own relative position. For example, the last Button is defined to lay to the left-of and aligned-with-the-top-of the View identified by the ID ok (which is the previous Button).
3.) Make sure you load this layout in the onCreate() method:
public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
}
The setContentView(int) method loads the layout file for the Activity, specified by the resource ID — R.layout.main refers to the res/layout/main.xml layout file.
4.) Run the application.
Steps to Create:
1.) Open Eclipse. Use the New Project Wizard and select Android Project Give the respective project name i.e. MyFirstRelativeLayout.

2.) Then enter the following information:
Project name: MyFirstRelativeLayout
Build Target: Android 1.6
Application name: MyFirstRelativeLayout
Package name: org.example. MyFirstRelativeLayout
Create Activity: MyFirstRelativeLayout

On Clicking Finish MyFirstRelativeLayout code structure is generated with the necessary Android Packages being imported along with MyFirstRelativeLayout.java.
Output –The final output:
OR
Here in this example we can manipulate the view as per our requirement i.e. if we want to adjust the size of button as per the length of button label then it will work. We can use the space as we need.






No comments:

Post a Comment