Thursday 28 March 2013

Create a Basic Form using Android SDK

Today, We are going to see how can create a Basic Form using Android SDK 4.2 . This Form will contain the following fields- Name, Email and Content.

Create a new Android Application project from eclipse with the following settings

Application Name: BasicForm
Project Name: BasicForm
Package Name: com.example.basicform
Activity Name: FormActivity
Android SDK: 4.2 (Choose a SDK that suits your test devices and favored emulator configuration)

In Package Explorer, create activity_form.xml file under res->layout folder and paste the below xml code.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
    tools:context=".FormActivity" >
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Fill The Form" />
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Name:" />
    <EditText
        android:id="@+id/name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10" >
        <requestFocus />
    </EditText>
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Email:" />
    <EditText
        android:id="@+id/email"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:inputType="textEmailAddress" />
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Content:" />
   <EditText
        android:id="@+id/content"
        android:layout_width="fill_parent"
        android:layout_height="80dp"
        android:ems="10"
        android:inputType="textMultiLine" />
    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Submit" />
</LinearLayout>

In Package Explorer, Create FormActivity.java file under src->com.example.basicform folder and paste the below code
package com.example.basicform;
import android.os.Bundle;
import android.app.Activity;

public class FormActivity extends Activity {

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_form);
  }
}

Activity is used to create an UI for the application, in other words its similar to creating a window.
setContentView is used to display the desired content inside the activity window. The desired content is placed inside the layout activity_form.xml File.

In AndroidManifest.xml, Place the below activity XML code inside <application>
<activity
            android:name="com.example.basicform.FormActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
</activity>

Thats it, Now you can run your application, by right clicking Package Name, then select Run As->Android Application. Based on my Android Emulator Configuration, i am getting the below Output.


Hope, you enjoyed this Post.

Download the Source Files Here

No comments:

Post a Comment