Android Tutorials - Creating a Layout in XML, How to create a layout with XML


Android Tutorials - Creating a Layout in XML, How to create a layout with XML

When we are create a new Android Application Project the project contains the number of sub folders and files including java classes, XML files, configuration files, some folders etc.

The project hierarchy look like as...




You see the several folders like src, gen, asset, bin, lib, res etc.
and if we expend these folders there are some other folders too inside these folders.

Now i am discuss about the resource folder if we will expand this folder there are number of methods like drawable, layout, menu, values etc and we can make our own folder in it.


  • The drawable folder is used to store the images in it.
  • The Layout folder defines the default layout for each activity.
  • The Menu folder is used to create the layout for menu items.
  • The values folder is used to assign the common styles and common values for the controls.
If we want to create a layout in XML then we need to create an Android XML file inside the layout folder.

The Example of a blank XML file look like as

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >
</RelativeLayout>

If we want to create a button in this layout then we need to add the code as below...


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >
    <Button android:layout_width="fill_parent" 
        android:layout_height="wrap_content"
        android:text="Load" android:id="@+id/btnLoad"/>

</RelativeLayout>



{ 0 comments... read them below or add one }

Post a Comment