Android Snippet – Simple ListView with ArrayAdapter

Android Snippet – Simple ListView with ArrayAdapter

In the android snippet example tutorial, we are going to learn how to create a simple ListView with ArrayAdapter in android application development. ListView is among the most popular and used View components in android.

ListView is a view group that displays a list of scrollable items. The list items are automatically inserted to the list using an Adapter that pulls content from a source such as an array or database query and converts each item result into a view that’s placed into the list.

We have covered Custom Adapter with ViewHolder Pattern in Android in one of our android code snippet example. We emphasis in the article the advantages of using ViewHolder Pattern. You can read more about it through the link above.

Before we start, the first thing I will do is to list the environment and tools I used in this android code snippet but feel free to use whatever environment or tools you are familiar with.

Windows 7

Android Studio

Samsung Galaxy Fame Lite

Min SDK 14

Target SDK 19

To create a new android application project, following the steps as stipulated below.

Go to File menu

Click on New menu

Click on Android Application

Enter Project name: ListViewWithArrayAdapter

Package: com.inducesmile.listviewwitharrayadapter

Keep other default selections.

Continue to click on next button until Finish button is active, then click on Finish Button

What we are going to do is to use an array to hold all the values that will use in our ListView, We will create an ArrayAdapter object that takes our data source and use the content of our data source and populate the ListView.

Once you are done with creating your project, copy and paste the following code snippet to your project. Replace the code in your file with the following code.

Make sure you change the package name if you did not use the same package.


import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class MainActivity extends ActionBarActivity {

private ListView customListView;


protected void onCreate(Bundle savedInstanceState) {



customListView = (ListView)findViewById(;

String [] listViewAdapterContent = {"Array 0", "Array 1", "Array 2", "Array 3", "Array 4"};

ArrayAdapter<String> listAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,, listViewAdapterContent);


customListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {


public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

// make Toast when click

Toast.makeText(getApplicationContext(), "Position " + position, Toast.LENGTH_LONG).show();





public boolean onCreateOptionsMenu(Menu menu) {

// Inflate the menu; this adds items to the action bar if it is present.

getMenuInflater().inflate(, menu);

return true;



public boolean onOptionsItemSelected(MenuItem item) {

// Handle action bar item clicks here. The action bar will

// automatically handle clicks on the Home/Up button, so long

// as you specify a parent activity in AndroidManifest.xml.

int id = item.getItemId();

//noinspection SimplifiableIfStatement

if (id == {

return true;


return super.onOptionsItemSelected(item);



The XML file used to create the ListView is showed below. You will find the XML code bolder with the layout code.

<RelativeLayout xmlns:android=""

xmlns:tools="" android:layout_width="match_parent"

android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"



android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">







android:layout_alignParentStart="true" />


So how did we achieve this?

1. We created the ListView by drag and drop a ListView component in our main layout.

2. In our activity file, we obtained the instance of our ListView by calling the findViewById method of the activity class.

3. We create an instance of ArrayAdapter with four parameters passed to it. The first one is the Context, then the in-built List item layout, then another in-built TextView that will holder the data in our ListView and finally the string Array that contains all our data.

4. We set the object of the ArrayAdapter as a parameter to setAdapter method of the ListView class

5. We attached an item click event. So whenever any item in the ListView is click, the position of the item will be displayed.

Save the file and run your project. If everything works for you, an image like this will appear on your test device.

Android Snippet - Simple ListView with ArrayAdapter

You can download the code for this tutorial below. If you are having hard time downloading the tutorials, kindly contact me.

Remember to subscribe with your email so that you will be among the first to receive our new post once it is published

Add a Comment