Download Code

Android Studio Custom ListView Example

In android studio custom listview example you will learn how to customize your listview within android application.Why you need this stuff because mostly clients like to see list of items with images.As we discuss in previous tutorial about basics of ListView.

Custom ListView

Now i am expanding the previous Listview tutorial to custom listview in android application.

Go ahead and create new android studio custom listview example android application project.After that go to your activity_main.xml file under res > Layout directory and add listview.

activity_main.xml

android studio custom listview,android studio custom listview example,custom listview example,custom listview,

You are done with simple task.Next what you have to do is somehow trciky 🙂 don’t get confused becuase i will lead you with guidelines.

To customize your listview you have to create new layout file called item.xml.After creating a layout i expect that you select LinearLayout with horizontal orientation.

Why horizontal orientation because you want to display an image with text in a single row.Add Imageview and Textview in LinearLayout.I have used an image of a hotel.

item.xml

android studio custom listview,android studio custom listview example,custom listview example,custom listview,

Done you have designed your listview according to somehow your need :).Next let’s deal with MainActivity.java class.

It’s important to tell you what you will do in your MainActivity.java class.Everything will be same expect two things.First is you will pass a complete layout to arrayAdapter,second that layout item id.You will basically use arrayAdapter constructor with four parameters.

  • Context
  • ResourseLayout
  • Item
  • List of data you can say array

MainActivity.java

package com.example.anonymous.studiocustomlistview;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    ListView codecList;   // list view variable
    // string array for list menu
    String androidCodec[] = {"Biryani","Kabab","Burger","Chinese Rice","Pepsi","Coke","Spirit"};
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        codecList = (ListView) findViewById(R.id.list);   // refering list view of xml
        //setting array adapter with item layout
        ArrayAdapter adapter = new ArrayAdapter(this, R.layout.item,R.id.itemTxt, androidCodec);
        codecList.setAdapter(adapter);   // setting adapter to list view
        // click event on list view items
        codecList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Toast.makeText(getApplicationContext(),androidCodec[position],Toast.LENGTH_SHORT).show();
            }
        });

    }


}

Compile and Run your android studio custom listview example application.

android studio custom listview,android studio custom listview example,custom listview example,custom listview,

That’s it for this tutorial.

Thanks.