Friday 19 June 2015

Custom Listview in android -2

No comments
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 same image Part-1
In this example we set a different 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

4) CustomListAdapter.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/list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
    </ListView>

</LinearLayout>

Step 2: Write code into mylist..xml
Created custom view for listview to show with image and text in list view.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >

    <ImageView
        android:id="@+id/icon"
        android:layout_width="60dp"
        android:layout_height="60dp"
        android:padding="5dp" />

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/item"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:layout_marginTop="5dp"
            android:padding="2dp"
            android:text="Medium Text"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:textColor="#0000aa" />

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:text="TextView" />
    </LinearLayout>

</LinearLayout>

Step 3: Write code into MainActivity.java

package dev.androidapplink.customlistviewapp_2;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.Toast;

public class MainActivity extends Activity {
// create variable
ListView list;
//define string for List item(s)
String[] itemname = { "pic1", "pic2", "pic3", "pic4", "pic5", "pic6",
"pic7", "pic8" };
//set image according to list item(s) from drawable
Integer[] imgid = { R.drawable.pic1, R.drawable.pic2, R.drawable.pic3,
R.drawable.pic4, R.drawable.pic5, R.drawable.pic6, R.drawable.pic7,
R.drawable.pic8, };

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//create adapter and bind item name with image id
CustomListAdapter adapter = new CustomListAdapter(this, itemname, imgid);
list = (ListView) findViewById(R.id.list);
list.setAdapter(adapter);
//implement on item click listener to show toast on click but you can put here your operation as your requirement of app
list.setOnItemClickListener(new OnItemClickListener() {

@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
String Slecteditem = itemname[+position];
// make your operation here i make toast.

Toast.makeText(getApplicationContext(), Slecteditem,
Toast.LENGTH_SHORT).show();

}
});
}

}

Step 4: Write code into CustomListAdapter.java
CustomListAdapter.java is a custom list adapter class which provides data to list view.
package dev.androidapplink.customlistviewapp_2;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class CustomListAdapter extends ArrayAdapter<String> {

private final Activity context;
private final String[] itemname;
private final Integer[] imgid;

public CustomListAdapter(Activity context, String[] itemname,
Integer[] imgid) {
super(context, R.layout.mylist, itemname);
// TODO Auto-generated constructor stub

this.context = context;
this.itemname = itemname;
this.imgid = imgid;
}

public View getView(int position, View view, ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
View rowView = inflater.inflate(R.layout.mylist, null, true);

TextView txtTitle = (TextView) rowView.findViewById(R.id.item);
ImageView imageView = (ImageView) rowView.findViewById(R.id.icon);
TextView extratxt = (TextView) rowView.findViewById(R.id.textView1);

txtTitle.setText(itemname[position]);
imageView.setImageResource(imgid[position]);
extratxt.setText("Description " + itemname[position]);
return rowView;
};

}
Step 5: Now Run Your Project:




No comments :

Post a Comment

Follow me Share