Custom listview in android

In this tutorial, I have explain about create simple custom listview android. By using  a custom list adapter. In normal listview you cannot add more data’s like images.

So we have use adapter to set the another view into the listview. By using adapter  you can  show multiple items inside the listview. Here, I have written simple coding for create custom listview android .

Steps to create custom listview in android

  • Create listview in the MainActivity.
  • Create adapter xml to set with the listview.
  • Create Listview Adapter to set the view to the listview. 
  • Set adapter to listview

Related Post

Listview item animation in android

Swipelistview in android

Edittext in listview android example


Step 1 – Create listview in the MainActivity

MainActivity.Java

public class MainActivity extends Activity
{
ListView listview;
@Override
protected void onCreate(Bundle savedInstanceState)
{ super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listview=(ListView)findViewById(R.id.listview);
}
}

main_activity.xml

<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=”.MainActivity” >
<ListView
android:id=”@+id/listview”
android:layout_width=”match_parent”
android:layout_height=”wrap_content”
android:layout_alignParentLeft=”true”
android:layout_alignParentTop=”true” >
</ListView>
</RelativeLayout>

Step 2 – Create adapter xml to set with the listview

adapter.xml

<?xml version=”1.0″ encoding=”utf-8″?>
<RelativeLayout xmlns:android=”http://schemas.android.com/apk/res/android” android:id=”@+id/RelativeLayout1″
android:layout_width=”match_parent”
android:layout_height=”match_parent”
android:orientation=”vertical” >
<RelativeLayout
android:id=”@+id/relativeLayout2″
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_alignParentLeft=”true”
android:layout_alignParentRight=”true” >
<ImageView android:id=”@+id/imageView1″
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:src=”@drawable/ic_launcher” />
<TextView android:id=”@+id/textView1″
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_centerVertical=”true”
android:layout_toLeftOf=”@+id/imageView2″ android:layout_toRightOf=”@+id/imageView1″
android:text=”Large Text” android:textAppearance=”?android:attr/textAppearanceLarge” />
<ImageView android:id=”@+id/imageView2″
android:layout_width=”35dp”
android:layout_height=”35dp”
android:layout_centerVertical=”true”
android:layout_alignParentRight=”true”
android:layout_alignParentTop=”true”
android:src=”@drawable/android_icon” />
</RelativeLayout> </RelativeLayout>

Step 3 – Create Listview Adapter to set the view to the listview

public class ListviewAdapter extends BaseAdapter
{
LayoutInflater inflater;
Context context;
public ListviewAdapter(Context context)
{
this.context=context;
//get the layout inflater
inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount()
{
// TODO Auto-generated method stub
return Dec.data.length;
}
@Override
public Object getItem(int arg0)
{
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int arg0)
{
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(int position, View view, ViewGroup parent)
{ // TODO Auto-generated method stub
ViewHolder holder;
/* A ViewHolder object stores each of the component views inside the tag field of the Layout, so you can immediately access them without the need to look them up repeatedly */
if (view == null)
{
holder = new ViewHolder();
//Instantiates a layout XML file into its corresponding View objects.
view = inflater.inflate(R.layout.adapter, null);
holder.data = (TextView) view.findViewById(R.id.textView1);
holder.image=(ImageView)view.findViewById(R.id.imageView1);
holder.icon=(ImageView)view.findViewById(R.id.imageView2);
// the setTag is used to store the data within this view
view.setTag(holder);
}
else
{
// the getTag returns the viewHolder object set as a tag to the view
holder = (ViewHolder)view.getTag();
}
holder.data.setText(Dec.data[position]); holder.image.setBackgroundResource(Dec.images[position]);holder.icon.setBackgroundResource(Dec.icons[position]);
return view;
}
public class ViewHolder
{ TextView data;
ImageView image,icon;
}
}

Step 4 – Set adapter to listview

public class MainActivity extends Activity
{
ListView listview;
@Override
protected void onCreate(Bundle savedInstanceState)
{ super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listview=(ListView)findViewById(R.id.listview);
//set customize adapter with the listview..
listview.setAdapter(new ListviewAdapter(this));
}
}
Download

3 thoughts on - Custom listview in android

Leave a Reply

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