Friday 19 October 2012

ANDROID - Custom ListView with Image and Text

Demo:

1)Custom ListView Activity:

package com.venky.customlistviewwithimageandtextview;

import android.app.Activity;
import android.os.Bundle;
import android.widget.ListView;

public class CustomListViewwithImageandTextViewActivity extends Activity {
      /** Called when the activity is first created. */
      final int[] my = { R.drawable.a1, R.drawable.a2, R.drawable.a3,
                  R.drawable.a4, R.drawable.a5, R.drawable.a6, R.drawable.a7,
                  R.drawable.a8 };

      ListView lvList;

      String[] name, phone;

      @Override
      public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);

            lvList = (ListView) findViewById(R.id.lvList);

            name = getResources().getStringArray(R.array.name);
            phone = getResources().getStringArray(R.array.phno);

            lvList.setAdapter(new MyAdapter(my, name, phone,
                        CustomListViewwithImageandTextViewActivity.this));

      }
}  

                    

2)MyAdapter:

package com.venky.customlistviewwithimageandtextview;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class MyAdapter extends BaseAdapter {
      String[] name, phone;
      Context context;
      int[] my;

      public MyAdapter(int[] my, String[] name, String[] phone,
                  CustomListViewwithImageandTextViewActivity myContext) {
            // TODO Auto-generated constructor stub

            this.my = my;
            this.name = name;
            this.phone = phone;
            context = myContext;
      }

      @Override
      public int getCount() {
            // TODO Auto-generated method stub
            return name.length;
      }

      @Override
      public Object getItem(int position) {
            // TODO Auto-generated method stub
            return position;
      }

      @Override
      public long getItemId(int position) {
            // TODO Auto-generated method stub
            return position;
      }

      @Override
      public View getView(int position, View v, ViewGroup parent) {
            // TODO Auto-generated method stub
            v = (LinearLayout) LayoutInflater.from(context).inflate(R.layout.style,
                        null);
            TextView tvName = (TextView) v.findViewById(R.id.tvName);
            TextView tvPhone = (TextView) v.findViewById(R.id.tvPhone);
            ImageView ivImage = (ImageView) v.findViewById(R.id.ivImage);
            tvName.setText(name[position]);
            tvPhone.setText(phone[position]);
            ivImage.setImageResource(my[position]);
            return v;
      }

}

3)main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#5F04B4"
    android:orientation="vertical" >

    <ListView
        android:id="@+id/lvList"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >
    </ListView>

</LinearLayout>


4)style.xml:

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

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center_vertical"
        android:orientation="horizontal" >

        <ImageView
            android:id="@+id/ivImage"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:layout_gravity="center" />

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:orientation="vertical" >

            <TextView
                android:id="@+id/tvName"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:gravity="center"
                android:textSize="20dp" />

            <TextView
                android:id="@+id/tvPhone"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:gravity="center"
                android:textSize="20dp" />
        </LinearLayout>
    </LinearLayout>

</LinearLayout>


5)Add this code your strings.xml:

<string-array name="name">
        <item>Nagendram</item>
        <item>Srinivasa Rao</item>
        <item>Saraswathi</item>
        <item>Mounika</item>
        <item>Chandra Shekar</item>
        <item>Subba Rao</item>
        <item>Venkateswarlu</item>
        <item>Hari</item>
    </string-array>

    <string-array name="phno">
        <item>0123456789</item>
        <item>1234567890</item>
        <item>2345678901</item>
        <item>3456789012</item>
        <item>9876543210</item>
        <item>8765432109</item>
        <item>7654321098</item>
        <item>1237894560</item>
    </string-array>



6)Download this Project Click Here

No comments:

Post a Comment