Introduction
This article explains how to create a custom ListView in Android
.
ListView
A ListView shows data in a list. The ListView loads the data using the Adapter class. The SetAdapter() method sets the data in a ListView.
In this application, I have created the customized ListView that contains images and text.
Use the following procedure to create the project.
Go to "File" -> "AndroidApplicationProject".
Step 1
Create an XML file activty_main with the following.
This XML file contains the ListView that you will to the MainActivty.
- <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"
- tools:context=".CustomListViewAndroidExample" >
- <ListView
- android:id="@+id/list"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:layout_weight="1"/>
- </RelativeLayout>
Step 2
Create another XML file customadapter.xml with the following.
This layout will inflate to the list view at run time. In this XML file, I took two image views and a text view. In the image view, I will the images and a link inside the text view.
- <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:layout_gravity="top"
- android:background="#cccccc"
- android:paddingTop="0dip" >
- <TableRow>
- <LinearLayout
- android:layout_width="110dp"
- android:layout_height="match_parent"
- android:paddingBottom="5dp"
- android:paddingLeft="10dp"
- android:paddingTop="5dp" >
- <ImageView
- android:id="@+id/image"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_gravity="left|top"
- android:scaleType="centerCrop" />
- </LinearLayout>
- <TableLayout
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:layout_gravity="top"
- android:paddingTop="0dip" >
- <TableRow>
- <TextView
- android:id="@+id/text"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_gravity="left|center_vertical"
- android:layout_marginLeft="10dip"
- android:layout_marginTop="4dip"
- android:layout_span="1"
- android:layout_weight="1"
- android:textColor="#000000"
- android:textSize="16sp" />
- </TableRow>
- <TableRow>
- <TextView
- android:id="@+id/text1"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_gravity="left|center_vertical"
- android:layout_marginLeft="10dip"
- android:layout_marginTop="4dip"
- android:layout_weight="1"
- android:gravity="left"
- android:text=""
- android:textColor="#000000"
- android:textSize="16sp" />
- </TableRow>
- </TableLayout>
- <ImageView
- android:layout_width="30dp"
- android:layout_height="30dp"
- android:layout_marginLeft="10dp"
- android:layout_marginTop="10dp"
- android:background="@drawable/imagesthumb"
- android:gravity="left"
- android:scaleType="centerCrop" />
- </TableRow>
- </TableLayout>
In MainActivity you will first get the layout by calling the setContentView() method like this setContentView(R.layout.activity_main). Now create a setDataList() that set the data.
- public void setDataList()
- {
- for (int i = 0; i < 11; i++)
- {
- final ListModel listModel = new ListModel();
-
- listModel.setCompanyName("Company " + i);
- listModel.setImage("image" + i);
- listModel.setUrl("http:\\www." + i + ".com");
-
- CustomArrlist.add(listModel);
- }
- }
After setting the data you will create an object of the CustomAdapter class. On creating the object of the CustomAdapter class the constructor of the CustomAdapter class will be called. You will the Activity, ListView, and res to the constructor of the CustomAdapter class.
- setDataList();
- Resources res = getResources();
- list = (ListView) findViewById(R.id.list);
In this class, you will create a method called onItemClick() so when you click the item in the list a toast message will appear.
- public void onItemClick(int mPosition)
- {
- ListModel tempValues = (ListModel) CustomArrlist.get(mPosition);
-
- Toast.makeText(cActivity, "" + tempValues.getCompanyName() + " Image:" + tempValues.getImage() + " Url:" + tempValues.getUrl(), Toast.LENGTH_LONG).show();
- }
Step 3
Create a Java class MainActivity file with the following:
- package com.customlistview;
- import java.util.ArrayList;
- import android.app.Activity;
- import android.content.res.Resources;
- import android.os.Bundle;
- import android.widget.ListView;
- import android.widget.Toast;
- public class MainActivity extends Activity
- {
- ListView list;
- CustomAdapter customadapter;
- public MainActivity cActivity = null;
- public ArrayList < ListModel > CustomArrlist = new ArrayList < ListModel > ();
-
- @Override
- protected void onCreate(Bundle savedInstanceState)
- {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- cActivity = this;
-
- setDataList();
- Resources res = getResources();
- list = (ListView) findViewById(R.id.list);
-
- customadapter = new CustomAdapter(cActivity, CustomArrlist, res);
- list.setAdapter(customadapter);
- }
-
- public void setDataList()
- {
- for (int i = 0; i < 11; i++)
- {
- final ListModel listModel = new ListModel();
-
- listModel.setCompanyName("Company " + i);
- listModel.setImage("image" + i);
- listModel.setUrl("http:\\www." + i + ".com");
-
- CustomArrlist.add(listModel);
- }
- }
-
- public void onItemClick(int mPosition)
- {
- ListModel tempValues = (ListModel) CustomArrlist.get(mPosition);
- Toast.makeText(cActivity, "" + tempValues.getCompanyName() + " Image:" + tempValues.getImage() + " Url:" + tempValues.getUrl(), Toast.LENGTH_LONG).show();
- }
- }
In a CustomAdapter class, you will extend the BaseAdapter class and implement onClickListener. The constructor will give you the MainActivty, ArrayList, and resource. Now count the data in the arraylist by calling the getCount() method.
Step 4
Create another Java class file with the following:
Step 5
Create a Java class file named ListModel with the following:
- package com.customlistview;
- public class ListModel
- {
- private String CompName = "";
- private String Image = "";
- private String Url = "";
-
- public void setComName(String CompName)
- {
- this.CompName = CompName;
- }
- public void setImg(String Image)
- {
- this.Image = Image;
- }
- public void setUrl(String Url)
- {
- this.Url = Url;
- }
-
- public String getCompanyName()
- {
- return this.CompName;
- }
- public String getImage()
- {
- return this.Image;
- }
- public String getUrl()
- {
- return this.Url;
- }
- }
Step 6
Android Manifest.xml file
- <?xml version="1.0" encoding="utf-8"?>
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="com.customlistview"
- android:versionCode="1"
- android:versionName="1.0" >
- <uses-sdk
- android:minSdkVersion="8"
- android:targetSdkVersion="18" />
- <application
- android:allowBackup="true"
- android:icon="@drawable/ic_launcher"
- android:label="@string/app_name"
- android:theme="@style/AppTheme" >
- <activity
- android:name="com.customlistview.MainActivity"
- 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>
Step 7