Wednesday 17 October 2012

ANDROID - ListView Custamization


1)Custom ListView Activity:


package com.venky.customlistview;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class CustomListViewActivity extends Activity {
      /** Called when the activity is first created. */

      String[] name, phno;

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

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

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

            MyAdapter adapter = new MyAdapter();
            lvList.setAdapter(adapter);
      }

      class MyAdapter extends BaseAdapter {

            @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 0;
            }

            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                  // TODO Auto-generated method stub

                  convertView = getLayoutInflater().inflate(R.layout.style, null);

                  TextView tv1 = (TextView) convertView.findViewById(R.id.tvName);
                  TextView tv2 = (TextView) convertView.findViewById(R.id.tvPhone);

                  tv1.setText(name[position]);
                  tv2.setText(phno[position]);

                  return convertView;
            }
      }
}

2)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:orientation="vertical" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@color/myColor"
        android:gravity="center"
        android:text="Custamized ListView"
        android:textSize="25dp"
        android:textStyle="bold" />

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

</LinearLayout>

3)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" >

    <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>


4)Add this code in your strings.xml:


<color name="myColor">#FF0000 </color>
      <color name="myBackgroundColor">#A251E9</color>

    <string-array name="name">
        <item>Venkateswarlu</item>
        <item>Srinivasa Rao</item>
        <item>Sarswathi</item>
        <item>Nagendram</item>
        <item>Subba Rao</item>
        <item>Ramya Krishna</item>
        <item>Sudhee kumar</item>
        <item>Hari Krishna</item>
        <item>Raj kumar</item>
        <item>Ashok</item>
    </string-array>
    <string-array name="phone">
        <item>0123456789</item>
        <item>1234567890</item>
        <item>4567891230</item>
        <item>7894561230</item>
        <item>9876543210</item>
        <item>6549873210</item>
        <item>1023456789</item>
        <item>No Number</item>
        <item>Not given</item>
        <item>secreat</item>
    </string-array>


5)Download this Project Click Here

No comments:

Post a Comment