How to Create a Simple Listview in Android
It is used to display the collection of items in the scrollable list form in android. It is implemented as follows :-
Create the main xml file which contains the list view named as
xmlns:tools=”http://schemas.android.com/tools” android:layout_width=”match_parent”
android:layout_height=”match_parent” android:paddingLeft=”@dimen/activity_horizontal_margin”
android:paddingRight=”@dimen/activity_horizontal_margin”
android:paddingTop=”@dimen/activity_vertical_margin”
android:paddingBottom=”@dimen/activity_vertical_margin” tools:context=”.MainActivity”>
android:layout_width=”match_parent”
android:layout_height=”wrap_content”
android:id=”@+id/listView1″
Create another xml file which contains the view which are contain in the single row of our list i.e prepare a layout of single item of list named as Mylistitm .xml as follows:-
Here one listiem of list contains
So prepare it according to the requirements.
android:layout_width=”wrap_content” android:layout_height=”match_parent”>
android:layout_width=”100dp”
android:layout_height=”100dp”
android:layout_marginTop=”20dp”
android:layout_marginLeft=”20dp”
android:src=”@drawable/images”/>
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:text=”Ayushi jain”
android:layout_marginLeft=”10dp”
android:id=”@+id/tv_name”
android:layout_marginTop=”25dp”
android:layout_toRightOf=”@+id/img_dp”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:text=”speciality:”
android:id=”@+id/tv_speclity”
android:drawablePadding=”10dp”
android:drawableRight=”@drawable/image1″
android:layout_marginTop=”2dp”
android:layout_toRightOf=”@+id/img_dp”
android:layout_below=”@+id/tv_name”
android:layout_marginLeft=”10dp”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:text=”aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa”
android:singleLine=”false”
android:layout_marginRight=”10dp”
android:layout_toRightOf=”@+id/img_dp”
android:layout_below=”@+id/tv_speclity”
android:layout_marginLeft=”10dp”
android:layout_marginTop=”4dp”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:text=”Skill level:”
android:id=”@+id/tv_skill”
android:layout_below=”@+id/tv_msg”
android:layout_alignLeft=”@+id/tv_msg”
android:layout_alignStart=”@+id/tv_msg” />
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
style=”?android:attr/ratingBarStyleSmall”
android:id=”@+id/ratingBar”
android:layout_toRightOf=”@+id/tv_skill”
android:layout_alignTop=”@+id/tv_skill”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:drawableRight=”@drawable/image1″
android:drawablePadding=”4dp”
android:id=”@+id/intrest”
android:layout_marginTop=”10dp”
android:layout_below=”@+id/img_dp”
android:layout_alignLeft=”@+id/img_dp”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_toRightOf=”@+id/intrest”
android:layout_below=”@+id/img_dp”
android:layout_marginTop=”10dp”
android:layout_marginLeft=”4dp”
android:src=”@drawable/image1″/>
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_toRightOf=”@+id/img1″
android:layout_below=”@+id/img_dp”
android:layout_marginTop=”10dp”
android:layout_marginLeft=”4dp”
android:src=”@drawable/image1″
android:id=”@+id/imageView” />
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:paddingTop=”10dp”
android:layout_marginTop=”20dp”
android:layout_marginRight=”10dp”
android:id=”@+id/tv_cost”
android:layout_above=”@+id/tv_speclity”
android:layout_alignParentRight=”true”
android:layout_alignParentEnd=”true”
android:drawablePadding=”10dp”
android:drawableRight=”@drawable/globe_icon”/>
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:id=”@+id/tv_time”
android:gravity=”center_vertical”
android:drawableLeft=”@drawable/globe_icon”
android:layout_marginLeft=”7dp”
android:layout_marginRight=”10dp”
android:layout_below=”@+id/tv_skill”
android:layout_alignParentRight=”true”
android:layout_alignParentEnd=”true” />
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:id=”@+id/tv_distance”
android:gravity=”center_vertical”
android:layout_below=”@+id/tv_skill”
android:drawableLeft=”@drawable/globe_icon”
android:layout_toLeftOf=”@+id/tv_time”
Create a adapter class which act as interface between list view and its data which will be displayed in it. This class provides the view of row to each item of class. Write the code as:-
package com.example.ayushi.demoscreen;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;SS
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.ArrayList;
* Created by ayushi on 9/4/15.
public class ScreenAdapter extends BaseAdapter {
ArrayList arrayList = new ArrayList();
ScreenAdapter(Context context, ArrayList arrayList) {
this.arrayList = arrayList;
public Object getItem(int position) {
public long getItemId(int position) {
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = LayoutInflater.from(context);
if (convertView == null) {
convertView = inflater.inflate(R.layout.item, parent, false);
holder.iv_dp = (ImageView) convertView.findViewById(R.id.img_dp);
holder.tv_name = (TextView) convertView.findViewById(R.id.tv_name);
holder.tv_msg= (TextView) convertView.findViewById(R.id.tv_msg);
holder.tv_cost= (TextView) convertView.findViewById(R.id.tv_cost);
holder.tv_time= (TextView) convertView.findViewById(R.id.tv_time);
holder.tv_distance= (TextView) convertView.findViewById(R.id.tv_distance);
convertView.setTag(holder);
holder = (Holder) convertView.getTag();
holder.iv_dp.setImageResource(arrayList.get(position).getDp_image());
holder.tv_name.setText(arrayList.get(position).getDp_name());
Create a bean class for set the value in array..
package com.example.ayushi.demoscreen;
* Created by ayushi on 9/4/15.
public class ScreenBean {
public ScreenBean(int dp_image, String dp_name, String message, String time, String distance, String cost) {
this.dp_image = dp_image;
this.distance = distance;
public int getDp_image() {
public void setDp_image(int dp_image) {
this.dp_image = dp_image;
public String getDp_name() {
public void setDp_name(String dp_name) {
public String getMessage() {
public void setMessage(String message) {
public String getTime() {
public void setTime(String time) {
public String getDistance() {
public void setDistance(String distance) {
this.distance = distance;
public String getCost() {
public void setCost(String cost) {
In the main activity, prepare the instance of array list which will create an object containing all the data which will be included in the layout of the one item of the list..After creating it bind it with the adapter for binding data with view….
package com.example.ayushi.demoscreen;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ListView;
import java.util.ArrayList;
public class MainActivity extends ActionBarActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv_list= (ListView) findViewById(R.id.listView1);
ArrayList arrayList=new ArrayList();
ScreenBean screenBean1=new ScreenBean();
screenBean1.setDp_image(R.drawable.images);
screenBean1.setDp_name(“Ayushi”);
screenBean1.setTime(“1.0pm”);
screenBean1.setDistance(“6miles”);
screenBean1.setMessage(“i am currently working on android”);
arrayList.add(screenBean1);
ScreenBean screenBean2=new ScreenBean();
screenBean2.setDp_image(R.drawable.images);
screenBean2.setDp_name(“Anu”);
screenBean2.setTime(“1.0pm”);
screenBean2.setDistance(“6miles”);
screenBean2.setMessage(“i am currently working on android”);
ScreenBean screenBean3=new ScreenBean();
screenBean3.setDp_image(R.drawable.images);
screenBean3.setDp_name(“tanu”);
screenBean3.setTime(“1.0pm”);
screenBean3.setDistance(“6miles”);
screenBean3.setMessage(“i am currently working on android”);
ScreenBean screenBean4=new ScreenBean();
screenBean4.setDp_image(R.drawable.images);
screenBean4.setDp_name(“Anu”);
screenBean4.setTime(“1.0pm”);
screenBean4.setDistance(“6miles”);
screenBean4.setMessage(“i am currently working on android”);
arrayList.add(screenBean1);
arrayList.add(screenBean2);
arrayList.add(screenBean3);
arrayList.add(screenBean4);
ScreenAdapter screenAdapter=new ScreenAdapter(this,arrayList);
lv_list.setAdapter(screenAdapter);
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return super.onOptionsItemSelected(item);
When we will run this program we will see a screen which contains the list with 4 list item…