Thursday 18 June 2015

Custom Listview in android-1

1 comment
In this chapter we learn how to implement custom Listview in android.Listview is one of the most popular controls, It can be used in many different forms and can be customized to suitable of app requirement.In this tutorial, I will demonstrate you how you can bind a simple array of Strings with ListView. 
May You Want To See Another Examples Of How To Create Simple Listview Part-1 and Part-2.
Customlistview with Different image Part-2.
In this example we set a same image for all list items.

Let's go for coding:


CREATE NEW ANDROID PROJECT


We need for this example:

1) activity_main.xml (...res/layout/ )
2) mylist.xml (...res/layout/ )
3) MainActivity.java

Step 1: Write code into activity_main.xml


This is a main activity where we put list view.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
    <ListView
        android:id="@+id/android:list"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />

</LinearLayout>

Step 2: Write code into mylist.xml

Created custom view for list view to show with image and text in list view.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal" >

    <ImageView

        android:id="@+id/icon"
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:layout_marginBottom="4dp"
        android:layout_marginLeft="4dp"
        android:layout_marginRight="4dp"
        android:layout_marginTop="4dp"
        android:src="@drawable/ic_launcher" />

    <TextView

        android:id="@+id/Itemname"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingTop="4dp"
        android:textSize="18sp" />


</LinearLayout>

Step 3: Write code into MainActivity.java


package dev.androidapplink.customlistviewapp;


import android.os.Bundle;

import android.app.ListActivity;
import android.widget.ArrayAdapter;

public class MainActivity extends ListActivity {

       
         //define string for List item(s)
String[] itemname = { "Android", ".NET", "JAVA", "FireFox",
"SQlite", "Orecale", "xmp", "Database" };

@Override

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
               
                //Bind custom list view by using ArrayAdapter
this.setListAdapter(new ArrayAdapter<String>(this, R.layout.mylist,
R.id.Itemname, itemname));
}

}
Step 4: Now Run Your Project:


Download project:

1 comment :

Follow me Share