Last updated on July 31st, 2016 at 11:16 am
Listview is very common and important in android most app use ListView to display data in list most common example Contact, Messenger etc. In this post i explain you how to create simple and custom listview in android. This post is focus on beginners and for those who want to learn how to create a simple and custom listview in android.
Simple ListView in Android
This is a simple application in which i use default android layout for ListView.
Creating New Project
1. Create new project in your Eclipse IDE / Android Studio and fill all the details File ⇒ New Project.
2. Open your main layout file inside res ⇒ layout ⇒ activity.xml. Now in this add listview.
<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=".MyActivity"> <ListView android:id="@+id/list" android:layout_width="match_parent" android:layout_height="match_parent"> </ListView> </RelativeLayout>
3. Now open your activity.java file inside src ⇒ com.package.name ⇒ Activity.java and write code to populate listView with data.
package com.customlist; import android.app.Activity; import android.os.Bundle; import android.util.Log; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.ListView; import android.widget.Toast; import java.util.ArrayList; public class MyActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_my); final ListView lt = (ListView)findViewById(R.id.list); String[] val = new String[]{"Android", "PHP", "Webdesign"}; ArrayList<String> list = new ArrayList<String>(); for(int i= 0; i < val.length; i++){ list.add(val[i]); } ArrayAdapter<String> ad = new ArrayAdapter(this, android.R.layout.simple_list_item_1, list); lt.setAdapter(ad); lt.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) { final String item = (String) adapterView.getItemAtPosition(i); Log.e("Error", "-----------------------------"+item); Toast.makeText(getApplicationContext(),"Position "+item, Toast.LENGTH_LONG).show(); } }); } }
In above code i create simple Array String and add that data to ArrayList after adding data to arraylist i use ArrayAdapter to add data to listview and also call default layout
ArrayAdapter<String> ad = new ArrayAdapter(this, android.R.layout.simple_list_item_1, list);
And after that i set data to Adapter using setAdapter.
Custom ListView in Android
Creating New Project
1. Create new project in your Eclipse IDE / Android Studio and fill all the details File ⇒ New Project.
2. Open your main layout file inside res ⇒ layout ⇒ activity.xml. Now in this add listview.
<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=".MyActivity"> <ListView android:id="@+id/list" android:layout_width="match_parent" android:layout_height="match_parent"> </ListView> </RelativeLayout>
3. Now create new xml layout file inside res ⇒ layout folder
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:id="@+id/tx" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </RelativeLayout>
this is the layout we pass in our listview.
4. Now first create a Custom java class in which we extend ArrayAdapter and pass String to listview. In this i also use LayoutInflater to inflate my view in list.
package com.customlist2; import android.app.Activity; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ArrayAdapter; import android.widget.TextView; /** * Created by aneh on 8/25/2014. */ public class Custom extends ArrayAdapter<String> { private final String[] text; private final Activity context; public Custom(Activity context, String[] text) { super(context, R.layout.design, text); this.text = text; this.context = context; } @Override public View getView(int position, View view, ViewGroup parent){ LayoutInflater inflate = context.getLayoutInflater(); View vie = inflate.inflate(R.layout.design, null, true); TextView txt = (TextView) vie.findViewById(R.id.tx); txt.setText(text[position]); return vie; } }
5. Now open your Mainactivity.java and create a array string, and create object of class pass class object to setAdapter.
package com.customlist2; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.AdapterView; import android.widget.ListView; import android.widget.Toast; public class MyActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_my); final String[] data = {"Aneh", "Test", "Thakur"}; Custom ct = new Custom(MyActivity.this, data); // ListView ListView list = (ListView)findViewById(R.id.list); list.setAdapter(ct); list.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { Toast.makeText(MyActivity.this, "You Clicked at " + data[position], Toast.LENGTH_SHORT).show(); } }); } }
In above i also use setOnItemClickListener to get click list item and display toast.