LUNCH LIST

BUILD UP A “LUNCH LIST” APPLICATION – ANDROID

A SIMPLE FORM

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. (LUNCH LIST)

Adobe Photoshop Tools and Panels

Step-By-Step Instructions

Here is how you’ll be able to create this application:

Step #1: Generate the Application Skeleton

First, we need to create a new project.

Eclipse

Use the new-project wizard to create an empty Android project named LunchList, 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. (LUNCH LIST)

Outside of Eclipse

Inside your terminal (e.g., Command Prompt for Windows), switch to a directory in which you would like the project to reside.  Then, run the following command:

android create project  –target   “Google  Inc.: Google  APIs:6” –path  ./LunchList –activity  LunchList  –package  apt.tutorial

This will produce an application skeleton for you, complete with everything you would like to begin building the Lunch List application. (LUNCH LIST)

Step #2: Modify the Layout

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>

 

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>

This gives us a three-row form: one row with a tagged field for the restaurant name, one with a tagged field for the restaurant address, and a big save button. (LUNCH LIST)

Step #3: Compile and Install the Application

Compile and install the application in the emulator by running the subsequent commands in your terminal:

ant  clean  install

Or, from Eclipse, just run the project.

Step #4: Run the Application in the Emulator

In your emulator, within the application launcher, you’ll see an icon for your Lunch List application. Click it to bring up your form:

101.pngFigure 1. The first edition of LunchList

Use the directional pad (D-pad) below the [MENU] button to navigate between the fields and buttons. Enter some text within the fields and click on the button, to visualize how those widgets behave. Then, click the back button to return to the application launcher. (LUNCH LIST)

Step #5: Create a Model Class

Now, we wish to feature a category to the project which will hold onto individual restaurants which will seem within the Lunch List. Right now, we can solely really work with one restaurant, but that will modification during a future tutorial.

So, using your text editor, produce a brand new file named LunchList/src/apt/tutorial/Restaurant.java with the subsequent 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;

}
}

This is merely a rudimentary model, with private information members for the name and address, and getters and setters for every of these.

Of course, don’t forget to save your changes!

Step #6: Save the Form to the Model

Finally, we wish to attach the Save button, specified once it’s pressed, we tend to update a restaurant object based on the 2 EditText fields. To do this, open up the LunchList/src/apt/tutorial/LunchList.java file and replace the generated Activity execution 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());

}

};

}

 

Here, we:

  • Create a single native restaurant instance once the activity is instantiated
  • Get our Button from the Activity via findViewById(), then connect it to a perceived to be otified once the button is clicked
  • In the perceiver, we get our 2 EditText widgets via findViewById(), then retrieve their contents and place them within the restaurant.

This code sample shows the utilization of an anonymous inner category implementation of a read.OnClickListener, named onSave. This method is employed in several places throughout this book because it is a convenient way to organize bits of custom code that go in these numerous perceiver objects.

Then, run the ant install command to compile and update the emulator. Run the application to create certain it sounds like it runs without errors, though’ at this point, we aren’t extremely using the info saved within the restaurant object just yet.

Extra Credit

Here are some things you can strive on the far side those bit-by-bit instructions:

  • Instead of using the console tools as documented above, try using Eclipse. You will need to download Eclipse, install the Android plugin, and use it to create your first project.
  • Try replacing the icon for your application. To do this, you will need to find a suitable 48×48 pixel image, create a drawable/directory inside your res/directory in the project, and adjust the AndroidManifest.xml file to contain an android: icon = “@drawable/my_icon”  attribute in the application element,  where my_icon is replaced by the base name of your image file.
  • Try playing with the fonts for use in both the TextView and EditText widgets. The Android SDK documentation will show a number of XML attributes you can manipulate to change the color, make the text boldface, etc.

YOUR FIRST ANDROID PROJECT

Image

This post contains content of the book Android Programming Tutorials by Mark L. Murphy below is the link of the complete book Android Programming Tutorials