ListView with custom layout in Android

June 13, 2012

In the article titled A ListView application in Android, we have used a built in layout to render the items in listview widget. In this article we are going to list items in a listview widget using a custom layout.

1. Create an Android project named ListViewCustomLayout

New Android Project

Figure 1 : Creating new Android Project

2. Select build target for this application

Select Android API Level

Figure 2 : Select API Level

3. Enter application details

Application Details

Figure 3: Application Details

4. Create a new layout file,  named “listview_layout” with TextView as the root element

ListView with custom Layout

Figure 4 : Create new layout file called listview_layout

5. Open the file /res/layout/listview_layout.xml and update the contents with the given below code :

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android=""
    android:id = "@+id/listview_item"

6. Open the file /res/layout/main.xml and update the contents with the given below code

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android=""
    android:orientation="vertical" >


7. Open the file /src/in/wptrafficanalyzer/listviewcustomlayout/ and update the contents with the given below code:

package in.wptrafficanalyzer.listviewcustomlayout;

import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class MainActivity extends Activity {

    String[] country = new String[] {
        "Sri Lanka",
        "North Korea",
        "South Korea",

    /** Called when the activity is first created. */
    public void onCreate(Bundle savedInstanceState) {

        // R.layout.listview_layout is the custom layout
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.listview_layout, country);

        ListView listView = (ListView) findViewById(;

8. Execute the application in Android Virtual Machine

ListView using Custom Layout

Figure 4 : Application in execution

9. Download the application source code

10. Testing with Android Testing Framework

Only the 50% of the application development is completed now. The remaining 50% is with the testing of the application and it is discussed in the article  titled “Android Testing Framework – Testing ListView with Custom Layout Application” .

11. Reference

How to hire me?

I am George Mathew, working as software architect and Android app developer at

You can hire me on hourly basis or on project basis for Android applications development.

For hiring me, please mail your requirements to

My other blogs

Android Knowledge Quiz

Ready to test your knowledge in Android? Take this quiz :


Leave a Reply

Your email address will not be published. Required fields are marked *

Be friend at g+

Subscribe for Lastest Updates

FBFPowered by ®Google Feedburner