Android ListView

Create the simple Listview application in Android. In last few years Android has taken the Tech World by storm. It is running on billions of devices and presents a great opportunity for professionals to innovate using this amazing technology. This project will be divided into 3 important parts: Android Installation, Creating a Hello World program and Creating a ListView Program

1) Let’s learn the android installation process:

  1. System requirements for Windows:
  • Microsoft® Windows® 8/7/Vista/2003 (32 or 64-bit)
  • 2 GB RAM minimum, 4 GB RAM recommended
  • 400 MB hard disk space
  • At least 1 GB for Android SDK, emulator system images, and caches
  • 1280 x 800 minimum screen resolution
  • Java Development Kit (JDK) 7
  • Optional for accelerated emulator: Intel® processor with support for Intel® VT-x, Intel® EM64T (Intel® 64), and Execute Disable (XD) Bit functionality
  1. Visit the given link http://developer.android.com/sdk/index.html

Android ListView

  1. Just click on the Download Android Studio button as shown above and accept the terms and conditions.

Android ListView

  1. Now click the download button and it will download Android Studio for you.
  2. Now double click on the downloaded file and install it in your machine.
  3. Once it gets installed it will display Welcome screen of Android Studio as shown below:

Android ListView

Now you have finished installation, now let’s move to next step

2) Creating a Hello World Program

  1. So, let’s create a new Android Studio project.
  2. Select Start a new Android Studio Project and a New Project Screen will get launched

Android ListView

  1. Now enter your Application name and click on Next
  2. Select the minimum required SDK from the list and click the Next button

Android ListView

  1. After clicking the Next button Add Activity Screen will be displayed

Android ListView

  1. Select the required Activity Screen and click on the Next button:

Android ListView

  1. Here is the window for your Activity creation with your Activity Name and Layout Name.

Android ListView

  1. Activity Name is your Java class name and Layout Name is your Xml file name.
  2. You can change the respective file names in this window and once done with the changes click on the Finish button available at the bottom-right corner.

Android ListView

  1. After clicking the Finish button Android Studio will start loading your project as shown below:

Android ListView

  1. Once its finished loading your project will get created :

Android ListView

  1. There are three important folders in the project which are Javaresmanifest.

Android ListView

  1. Java folder consists of your java classes, res folder consists of drawable folder layout folder values folder where the layout folder consists of your xml files.
  2. Whenever you create a new java class it is created under the java folder and your new xml file is created in your layout

Android ListView

  1. If you have a look on your xml file, you will find two tabs Design and Text.

Android ListView

  1. In Design tab you can design your layout by dragging and dropping the views from palette. This palette is placed at the left hand side from where you have to drag the views and place it on the layout as shown below:

Android ListView

  1. Now if you check out the text tab in xml file, you will find the code related to the xml you have designed.

Android ListView

  1. The controller code is to be written in java class as shown in the figure given below:

Android ListView

  1. To run your application you must have your Android Virtual Device created.
  2. To create your Android Virtual Device click on the AVD Manager as shown in the image given below:

Android ListView

  1. AVD Manager window will get displayed where you have to click on Create Virtual Device button:

Android ListView

  1. After clicking the Create Virtual Device a category list will be generated from which you can select your respective device and click on the Next

Android ListView

  1. Select the System Image and click on the Next button as shown in the image below:

Android ListView

  1. Give a proper name to your AVD and click on the finish button :

Android ListView

  1. After clicking the finish button your device will get created in the Android Virtual Device Manager:

Android ListView

  1. Once it gets created you can view your Android Virtual Device in the Android Virtual Device Manager window as shown in the image given below :

Android ListView

  1. Now just select your created Android Virtual Device and click on the OK button and your Android Virtual Device creation will be successful.
  2. You can also update your device by doing right click and selecting duplicate option where you can make changes in the device and can update it.
  3. Now to run your application, select your project and click on the Run

Android ListView

  1. After running your application your Android Virtual Device that is your emulator will get launched soon :

Android ListView

  1. Home Screen will be appeared in short time span, once we touch the applications button you will get the applications installed in it, which consists of the application that we developed.

Android ListView

  1. Once you select the application that we developed and click on it Android Virtual Device will display our designed Activity.

Android ListView

  1. This confirms that you are done with the Installation Successfully. Now we will move towards our final project

3) Creating a ListView program in Android

  1. Now let’s create a small list view application in android
  1. In this application we will be implementing List View which will generate list of items in it and selecting a specific item it will display a Toast message that the respective item is selected.
  1. So, first of all create a new project and make your MainActivity.java and activity_main.xml file ready.
  1. In your activity_main.xml file drag and drop a TextView and change its id in the layout as shown in the figure given below to display the list.

Android ListView

  1. Now to display some items in the list lets create xml file in res-> under values-> Rightclick on values folder -> New-> XML->Values XML File as shown below, we have named it as data.xml:

Android ListView

  1. In this xml file create a String array of name fruits and ad the items in it so that it will store all the items that are to be displayed in the list view.

data.xml file

01 <?xml version="1.0" encoding="utf-8"?>
02 <resources>
03     <string-array name="fruits">
04         <item>Mango</item>
05         <item>Ckikoo</item>
06         <item>Pomogranate</item>
07         <item>Custard Apple</item>
08         <item>Apple</item>
09         <item>Grapes</item>
10         <item>Guava</item>
11         <item>Orange</item>
12         <item>PineApple</item>
13         <item>Strawberry</item>
14         <item>Berrys</item>
15         <item>Jamun</item>
16         <item>Cucumber</item>
17         <item>Carrot</item>
18         <item>Banana</item>
19     </string-array>
20 </resources>
  1. Now let’s write the code in our MainActivity.java class to display the list along with the items.
  2. As we are creating a listview, just extend your MainActivity class with ListActivity .
  3. In your MainActivity.java write the given code:

Android ListView

Android ListView

MainActivity.Java

01 public class MainActivity extends ListActivity {
02   // Create a string Array
03   String[]fruits;
04   @Override
05   protected void onCreate(Bundle savedInstanceState) {
06     super.onCreate(savedInstanceState);
07     fruits = getResources().getStringArray(R.array.fruits);
08     this.setListAdapter(new ArrayAdapter<String>(
09       this,
10       R.layout.activity_main,
11       R.id.check,
12       fruits));
13     ListView listview = getListView();
14     //Adding item click listener on listview
15     listview.setOnItemClickListener(new AdapterView.OnItemClickListener(){
16       @Override
17       public void onItemClick(AdapterView<?> parent, View clickView, int position,
18       long id) {
19         // TODO Auto-generated method stub
20         // Store the fruits position in fruit which is declared as string
21         String fruit = fruits[position];
22         // Writting the toast message to display the message about which fruit is selected
23         Toast.makeText(MainActivity.this, String.format("%s Fruit is selected", fruit),
24         Toast.LENGTH_SHORT).show();
25         // Writting a switch statement to switch to new page on clicking the list items
26         switch (position)
27         {
28           case 0 : Intent intent = new Intent(MainActivity.this , NewActivity.class);
29             startActivity(intent);
30             break;
31         }
32       }
33     });
34   }
35 }
  1. Let’s have the explanation of the code:
  2. In line no 22 -> we are just getting the resources from the data.xml file that is the items stored in the array into a String named as fruits.
  3. Then in line 23-> Set the ListAdapter which will display the list on the layout by passing the parameters like your layout file, id of the textview, array of strings.
  4. Now just take a listview and create its object.
  5. In line no 33->Add an OnItemClickListener which will listen to the clicks done on the items in the list.
  6. In line no 40-> store the fruits position into a string named as fruit.
  7. Then to display the message that the respective fruit is selected we added Toast message in which we passed the string that was storing the fruits position.
  8. In line no 26-> We added a switch statement in which we passed position as a parameter hence it will detect the position of the click and will perform intent using the respective cases.
  9. Now to perform intent we need to create a new class which can be done through the following method:

Android ListView

  1. Similarly create a new xml file for your new Java class:

Android ListView

  1. Thus now in your project you will have your new java and xml files.

Android ListView

  1. Now make your new java class ready for execution by writing the onCreate method.

Android ListView

NewActivity.Java

1 public class NewActivity extends Activity {
2   @Override
3   protected void onCreate(Bundle savedInstanceState) {
4     super.onCreate(savedInstanceState);
5     setContentView(R.layout.newactivity);
6   }
7 }
  1. Do not forget to make an entry of your new class in Manifest file using the activity tag

Android ListView

  1. Thus, now the application is completed and you can run it and see the output on the emulator:

Android ListView

  1. And now if you click on an item from the list it will display the toast message and if you have defined the cases in the switch it will navigate to the respective page:

Android ListView

Thus, we have successfully studied how to install Android Studio on your machine using which we created a sample program and also developed a small ListView application in Android.


[sociallocker]

download sources

[/sociallocker]