Introduction
ListView is a control for presenting components in the form of a list. We can insert a TextBox (EditText), TextView (label), Button or other types of components into a ListView.
Today we will create a ListView like the following,

- <RelativeLayout
- 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:paddingBottom="@dimen/activity_vertical_margin"
- android:paddingLeft="@dimen/activity_horizontal_margin"
- android:paddingRight="@dimen/activity_horizontal_margin"
- android:paddingTop="@dimen/activity_vertical_margin"
- tools:context=".MainActivity" >
- <ListView>
- android:id="@+id/list"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content" ></ListView>
- </RelativeLayout>

- <?xml version="1.0" encoding="utf-8"?>
- <TableLayout
- xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:background="#ffbbcc">
- <TableRow>
- <ImageView
- android:id="@+id/img"
- android:layout_width="50dp"
- android:layout_height="50dp"/>
- <TextView
- android:id="@+id/txt"
- android:layout_width="wrap_content"
- android:layout_height="50dp" />
- </TableRow>
- </TableLayout>

In the res (resource) folder of this project, we will provide a number of images.

- package learn2crack.customlistview;
- import android.app.Activity;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.ArrayAdapter;
- import android.widget.ImageView;
- import android.widget.TextView;
- public class CustomList extends ArrayAdapter < String > {
- private final Activity context;
- private final String[] web;
- private final Integer[] imageId;
- public CustomList(Activity context, String[] web, Integer[] imageId)
- {
- super(context, R.layout.list_single, web);
- this.context = context;
- this.web = web;
- this.imageId = imageId;
- }@Override
- public View getView(int position, View view, ViewGroup parent)
- {
- LayoutInflater inflater = context.getLayoutInflater();
- View rowView = inflater.inflate(R.layout.list_single, null, true);
- TextView txtTitle = (TextView) rowView.findViewById(R.id.txt);
- ImageView imageView = (ImageView) rowView.findViewById(R.id.img);
- txtTitle.setText(web[position]);
- imageView.setImageResource(imageId[position]);
- return rowView;
- }
- }
Explanation of Code
- public CustomList(Activity context, String[] web, Integer[] imageId)
- {
- super(context, R.layout.list_single, web);
- this.context = context;
- this.web = web;
- this.imageId = imageId;
- }
Code Section 3
- public View getView(int position, View view, ViewGroup parent)
- {
- LayoutInflater inflater = context.getLayoutInflater();
- View rowView = inflater.inflate(R.layout.list_single, null, true);
- TextView txtTitle = (TextView) rowView.findViewById(R.id.txt);
- ImageView imageView = (ImageView) rowView.findViewById(R.id.img);
- txtTitle.setText(web[position]);
- imageView.setImageResource(imageId[position]);
- return rowView;
- }
This is an important part of this project. This method is responsible for inserting a view into the ListView control. This method is invoked implicitly by the system. In this method, we pass three parameters. The first parameter contains the position of a view in the ListView. The second parameter is an instance of the view and the third parameter is a ViewGroup. A ViewGroup is a special view that can contain other views (called children). The view group is the base class for layouts and view containers.
- import android.os.Bundle;
- import android.view.View;
- import android.widget.AdapterView;
- import android.widget.ListView;
- import android.widget.Toast;
- import android.app.Activity;
- public class MainActivity extends Activity
- {
- ListView list;
- String[] web =
- {
- "Google Plus",
- "Twitter",
- "Windows",
- "Bing",
- "Itunes",
- "Wordpress",
- "Drupal"
- };
- Integer[] imageId =
- {
- R.drawable.image1,
- R.drawable.image2,
- R.drawable.image3,
- R.drawable.image4,
- R.drawable.image5,
- R.drawable.image6,
- R.drawable.image7
- };
- @Override
- protected void onCreate(Bundle savedInstanceState)
- {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- CustomList adapter = new CustomList(MainActivity.this, web, imageId);
- list = (ListView) findViewById(R.id.list);
- list.setAdapter(adapter);
- list.setOnItemClickListener(new AdapterView.OnItemClickListener()
- {
- @Override
- public void onItemClick(AdapterView <? > parent, View view,
- int position, long id)
- {
- Toast.makeText(MainActivity.this, "You Clicked at " + web[+position], Toast.LENGTH_SHORT).show();
- }
- });
- }
- }
- String[] web =
- {
- "Google Plus",
- "Twitter",
- "Windows",
- "Bing",
- "Itunes",
- "Wordpress",
- "Drupal"
- };
- Integer[] imageId =
- {
- R.drawable.image1,
- R.drawable.image2,
- R.drawable.image3,
- R.drawable.image4,
- R.drawable.image5,
- R.drawable.image6,
- R.drawable.image7
- };
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- CustomList adapter = new CustomList(MainActivity.this, web, imageId);
- list = (ListView) findViewById(R.id.list);
- list.setAdapter(adapter);
Code Section 6
- list.setOnItemClickListener(new AdapterView.OnItemClickListener()
- {@Override
- public void onItemClick(AdapterView <? > parent, View view,
- int position, long id)
- {
- Toast.makeText(MainActivity.this, "You Clicked at " + web[+position], Toast.LENGTH_SHORT).show();
- }
- });



Pankaj Kumar ChoudharyPosted Jul 23, 2015, 7:59 PM
Thanks Santhakumar Sir........
Santhakumar MunuswamyPosted Jul 23, 2015, 3:07 PM
Thanks for nice article:)
Pankaj Kumar ChoudharyPosted Jul 22, 2015, 8:59 PM
Thanks Rahul Sir...........
Rahul Kumar SaxenaPosted Jul 22, 2015, 2:22 PM
Goo Show
Abhishek YadavPosted Jul 22, 2015, 12:42 PM
Great article Pankaj Kumar Choudhary!!!
Pankaj Kumar ChoudharyPosted Jul 22, 2015, 7:33 AM
Thanks Kalyankumar Sir..........
Kalyankumar RavichandranPosted Jul 22, 2015, 7:23 AM
Nice view!
Pankaj Kumar ChoudharyPosted Jul 21, 2015, 8:08 PM
Thanks Rakesh Sir.........
RakeshPosted Jul 21, 2015, 1:09 PM
Good one
Pankaj Kumar ChoudharyPosted Jul 21, 2015, 9:39 AM
Thanks Sibeesh Sir.........
Sibeesh VenuPosted Jul 21, 2015, 9:34 AM
Nice Share.
Nilesh JadavPosted Jul 21, 2015, 9:02 AM
Nice one sir
Manoj BhoirPosted Jul 21, 2015, 8:48 AM
Good One