Monday, August 16, 2010

use xml Layout in android activity

Now you are familiar with the HelloWorld application in Android. Now I want to use xml Layouts in my activities.

   1.  You will create a helloworld application with your desire name.
   2. Open the res>layout node.
   3. Double click on main.xml file.
   4. It will be open in right pane of the eclipse.
   5. You will see the tag with linear layout option.
   6. After this tag you will find the TextView tag.
   7. You will change its text property as "Enter Name".
   8. After the closing bracket </TextView> You press Enter.
   9. And put <EditText android:layout_height="wrap_content" android:layout_width="fill_parent"> </EditText>
  10. Above line of code create a Text Field for enter the text.
  11. Now, enter the text <Button  android:layout_height="wrap_content" android:layout_width="fill_parent" android:text="Login"></Button>
  12. Now, Run the application in Android Emulator.
  13. You will see the login screen.

The full code of the main.xml is as follow:


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Enter Name"
    />
    <EditText
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
     />
     <Button
         android:layout_width="fill_parent"
         android:layout_height="wrap_content"
         android:text="Login"
         />  
 
</LinearLayout>