Android Custom ListView Texts Tutorial
Last Updated: April 30, 2013
In this tutorial, you will learn how to create a custom listview with texts in your Android Applications. Using a custom adapter allows to you to customize listview item layouts. We will create a listview to show texts and on listview item click will show the selected results in a new activity. So lets begin…
Create a new project in Eclipse File > New > Android Application Project. Fill in the details and name your project CustomListView.
Application Name : CustomListView
Project Name : CustomListView
Package Name : com.androidbegin.customlistview
Open your MainActivity.java and paste the following code.
MainActivity.java
package com.androidbegin.customlistview; import android.os.Bundle; import android.app.Activity; import android.content.Intent; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.ListView; import android.view.View; public class MainActivity extends Activity { // Declare Variables ListView list; ListViewAdapter adapter; String[] rank; String[] country; String[] population; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Get the view from listview_main.xml setContentView(R.layout.listview_main); // Generate sample data into string arrays rank = new String[] { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10" }; country = new String[] { "China", "India", "United States", "Indonesia", "Brazil", "Pakistan", "Nigeria", "Bangladesh", "Russia", "Japan" }; population = new String[] { "1,354,040,000", "1,210,193,422", "315,761,000", "237,641,326", "193,946,886", "182,912,000", "170,901,000", "152,518,015", "143,369,806", "127,360,000" }; // Locate the ListView in listview_main.xml list = (ListView) findViewById(R.id.listview); // Pass results to ListViewAdapter Class adapter = new ListViewAdapter(MainActivity.this, rank, country, population); // Binds the Adapter to the ListView list.setAdapter(adapter); // Capture ListView item click list.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { Intent i = new Intent(MainActivity.this, SingleItemView.class); // Pass all data rank i.putExtra("rank", rank); // Pass all data country i.putExtra("country", country); // Pass all data population i.putExtra("population", population); // Pass listview item click position i.putExtra("position", position); // Open SingleItemView.java Activity startActivity(i); } }); } }
In this activity, we have created string arrays with sample data and pass it into the ListViewAdapter class. On listview item click will pass the selected position and string arrays to a new activity.
Next, create an XML graphical layout for your MainActivity. Go to res > layout > Right Click on layout > New > Android XML File
Name your new XML file listview_main.xml and paste the following code.
listview_main.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" > <ListView android:id="@+id/listview" android:layout_width="fill_parent" android:layout_height="fill_parent" /> </RelativeLayout>
Output:
Next, create a custom listview adapter. Go to File > New > Class and name it ListViewAdapter.java. Select your package named com.androidbegin.customlistview and click Finish.
Open your ListViewAdapter.java and paste the following code.
ListViewAdapter.java
package com.androidbegin.customlistview; import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.TextView; public class ListViewAdapter extends BaseAdapter { // Declare Variables Context context; String[] rank; String[] country; String[] population; LayoutInflater inflater; public ListViewAdapter(Context context, String[] rank, String[] country, String[] population) { this.context = context; this.rank = rank; this.country = country; this.population = population; } @Override public int getCount() { return rank.length; } @Override public Object getItem(int position) { return null; } @Override public long getItemId(int position) { return 0; } public View getView(int position, View convertView, ViewGroup parent) { // Declare Variables TextView txtrank; TextView txtcountry; TextView txtpopulation; inflater = (LayoutInflater) context .getSystemService(Context.LAYOUT_INFLATER_SERVICE); View itemView = inflater.inflate(R.layout.listview_item, parent, false); // Locate the TextViews in listview_item.xml txtrank = (TextView) itemView.findViewById(R.id.rank); txtcountry = (TextView) itemView.findViewById(R.id.country); txtpopulation = (TextView) itemView.findViewById(R.id.population); // Capture position and set to the TextViews txtrank.setText(rank[position]); txtcountry.setText(country[position]); txtpopulation.setText(population[position]); return itemView; } }
In this custom listview adapter class, string arrays are passed into the ListViewAdapter and set into the TextViews followed by the positions.
Next, create an XML graphical layout for your listview item. Go to res > layout > Right Click on layout > New > Android XML File
Name your new XML file listview_item.xml and paste the following code.
listview_item.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:id="@+id/ranklabel" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/ranklabel" /> <TextView android:id="@+id/rank" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_toRightOf="@+id/ranklabel" /> <TextView android:id="@+id/countrylabel" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/ranklabel" android:text="@string/countrylabel" /> <TextView android:id="@+id/country" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/rank" android:layout_toRightOf="@+id/countrylabel" /> <TextView android:id="@+id/populationlabel" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/countrylabel" android:text="@string/populationlabel" /> <TextView android:id="@+id/population" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/country" android:layout_toRightOf="@+id/populationlabel" /> </RelativeLayout>
Output:
Next, create a new activity to show results on listview item click. Go to File > New > Class and name it SingleItemView.java. Select your package named com.androidbegin.customlistview and click Finish.
Open your SingleItemView.java and paste the following code.
SingleItemView.java
package com.androidbegin.customlistview; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.widget.TextView; public class SingleItemView extends Activity { // Declare Variables TextView txtrank; TextView txtcountry; TextView txtpopulation; String[] rank; String[] country; String[] population; int position; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.singleitemview); // Retrieve data from MainActivity on listview item click Intent i = getIntent(); // Get the listview item click position position = i.getExtras().getInt("position"); // Get the list of rank rank = i.getStringArrayExtra("rank"); // Get the list of country country = i.getStringArrayExtra("country"); // Get the list of population population = i.getStringArrayExtra("population"); // Locate the TextViews in singleitemview.xml txtrank = (TextView) findViewById(R.id.rank); txtcountry = (TextView) findViewById(R.id.country); txtpopulation = (TextView) findViewById(R.id.population); // Load the text into the TextViews followed by the position txtrank.setText(rank[position]); txtcountry.setText(country[position]); txtpopulation.setText(population[position]); } }
In this activity, strings are retrieved from the ListViewAdapter by using Intent sets the TextViews.
Next, create an XML graphical layout for your SingleItemView. Go to res > layout > Right Click on layout > New > Android XML File
Name your new XML file singleitemview.xml and paste the following code.
singleitemview.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:id="@+id/ranklabel" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/ranklabel" /> <TextView android:id="@+id/rank" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_toRightOf="@+id/ranklabel" /> <TextView android:id="@+id/countrylabel" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/ranklabel" android:text="@string/countrylabel" /> <TextView android:id="@+id/country" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/rank" android:layout_toRightOf="@+id/countrylabel" /> <TextView android:id="@+id/populationlabel" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/countrylabel" android:text="@string/populationlabel" /> <TextView android:id="@+id/population" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/country" android:layout_toRightOf="@+id/populationlabel" /> </RelativeLayout>
Next, change the application name and texts. Open your strings.xml in your res > values folder and paste the following code.
strings.xml
<resources> <string name="app_name">CustomListView</string> <string name="hello_world">Hello world!</string> <string name="menu_settings">Settings</string> <string name="ranklabel">"Rank : "</string> <string name="countrylabel">"Country : "</string> <string name="populationlabel">"Population : "</string> </resources>
In your AndroidManifest.xml, we need to declare an activity. Open your AndroidManifest.xml and paste the following code.
AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.androidbegin.customlistview" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="15" /> <application android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name=".MainActivity" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name=".SingleItemView" > </activity> </application> </manifest>
Output :
Source Code
[purchase_link id=”7906″ text=”Purchase to Download Source Code” style=”button” color=”green”]
cheers,it remembers me of Symbian...
Wei Huang
Android Custom ListView Texts Tutorial
how to make all list view items click listener outside an adapter
Androd Mangalore
Android Custom ListView Texts Tutorial
Great tutorial. It's helped me lots. Thank you. I have done this tutorial with Fragment. :)
Fahry Mohammed
Android Custom ListView Texts Tutorial
Dear Sir The Same tutorial but its papolated from mysql using json
Muhammed Hussain
Android Custom ListView Texts Tutorial