Android - Creating a simple form (Eclipse)

This tutorial is the first of several that will build up a lunch list application, where you can track  various likely places to go to lunch. While this application may seem silly, it will give you a chance  to exercise many features of the Android platform. Besides, perhaps you may even find the application to be useful someday.

Generate the Application Skeleton

  1. Create New Project(Refer here)
    Use the new-project wizard to create an empty Android project named LunchList, as described in the Link above. This will create an application skeleton for you, complete with everything  you need to build your first Android application: Java source code, build instructions, etc.

    In particular:
    • Choose a build target that is API Level 9 or higher and has the Google APIs, so you can add a map to the application later.
    • Name the project LunchList, with an initial activity also named LunchList
    • Use apt.tutorial for the package name

Modify the Layout

  1. Using your text editor, open the LunchList/res/layout/main.xml file. Initially, that file will look like this:
    <?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="Hello World, LunchList"
    />
    </LinearLayout>

  2. Change that layout to look like this:
     <?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"
    >
    <LinearLayout
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    >
    <TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Name:"
    />
    <EditText android:id="@+id/name"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    />
    </LinearLayout>
    <LinearLayout
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    >
    <TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Address:"
    />
    <EditText android:id="@+id/addr"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    />
    </LinearLayout>
    <Button android:id="@+id/save"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Save"
    />
    </LinearLayout>
  3. This gives us a three-row form: one row with a labeled field for the restaurant name, one with a labeled field for the restaurant address, and a big Save button.

Support All Screen Sizes

    You may want to test this application on an emulator. You may want to test it on a phone.You may want to test it on a tablet. The layouts we use in these tutorials will work on a variety of screen sizes, but they will work better if we tell Android that we do indeed those screen sizes. To that end, we need to modify the manifest for our project, to add a <supports-screens> element, declaring what sizes we support and do not.

    1. Open the AndroidManifest.xml file in the root of your project tree, and add in a <supports-screens> element. The resulting file should resemble:
      <?xml version="1.0" encoding="utf-8"?>
      <manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="apt.tutorial"
      android:versionCode="1"
      android:versionName="1.0">
      <supports-screens
      android:xlargeScreens="true"
      android:largeScreens="true"
      android:normalScreens="true"
      android:smallScreens="false"
      />
      <application android:label="@string/app_name">
      <activity android:name=".LunchList"
      android:label="@string/app_name">
      <intent-filter>
      <action android:name="android.intent.action.MAIN" />
      <category android:name="android.intent.category.LAUNCHER" />
      </intent-filter>
      </activity>
      </application>
      </manifest>
    2. Here, we are declaring that we support normal, large, and extra-large screens, but NOT SMALL screens. Android will not automatically scale down our UI, so our application will not run on a small-screen device (typically under 3" diagonal screen size). However, it will run well on everything bigger than that.

    Compile and Install the Application

    Run the project :
    In your emulator, in the application launcher, you will see an icon for your LunchList application. Click it to bring up your form:

    Use the directional pad (D-pad) to navigate between the fields and button. Enter some text in the  fields and click the button, to see how those widgets behave. Then, click the BACK button to return to the application launcher.

    Create a Model Class

    Now, we want to add a class to the project that will hold onto individual restaurants that will appear in the LunchList. Right now, we can only really work with one restaurant, but that will change in a future tutorial.
    1. using your text editor, create a new file named LunchList/src/apt/tutorial/Restaurant.java with the following contents:
      package apt.tutorial;

      public class Restaurant {
      private String name="";
      private String address="";
      public String getName() {
      return(name);
      }
      public void setName(String name) {
      this.name=name;
      }
      public String getAddress() {
      return(address);
      }
      public void setAddress(String address) {
      this.address=address;
      }
      }

      Save the Form to the Model

      Finally, we want to hook up the Save button, such that when it is pressed, we update a restaurant  object based on the two EditText fields. To do this, open up the LunchList/src/apt/tutorial/LunchList.java file and replace the generated Activity implementation with the one shown below:

      package apt.tutorial;


      import android.app.Activity;
      import android.os.Bundle;
      import android.view.View;
      import android.widget.Button;
      import android.widget.EditText;


      public class LunchList extends Activity {
            Restaurant r=new Restaurant(); 

            @Override
            public void onCreate(Bundle savedInstanceState) {
                    super.onCreate(savedInstanceState);
                    setContentView(R.layout.main);
                    Button save=(Button)findViewById(R.id.save);
                    save.setOnClickListener(onSave);
             } 

             private View.OnClickListener onSave=new View.OnClickListener() {
                    public void onClick(View v) {
                          EditText name=(EditText)findViewById(R.id.name);
                          EditText address=(EditText)findViewById(R.id.addr);
                          r.setName(name.getText().toString());
                          r.setAddress(address.getText().toString());
                   }
             };
      }


      Code Explaination
      • Create a single local restaurant instance when the activity is instantiated.
      • Get our Button from the Activity via findViewById(), then connect it to a listener to be notified when the button is clicked
      • In the listener, we get our two EditText widgets via findViewById(),then retrieve their contents and put them in the restaurant

      Run the application to make sure it seems like it runs without errors, though at this point we are not really using the data saved in the restaurant object just yet.


      Reference :

      By
      NOTE : – If You have Found this post Helpful, I will appreciate if you can Share it on Facebook, Twitter and Other Social Media Sites. Thanks =)

      Popular posts from this blog

      Example to disable save as certain file type in SSRS Report Viewer

      Phone book application example - Java

      Modal Popup Message Box ASP.NEt C# Example