Wednesday 17 October 2012

ANDROID - ListView Custamization 1



1)Custom ListView Activity:


package com.venky.customlistview1;

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

public class CustomListView1Activity extends Activity {
      /** Called when the activity is first created. */
      ListView lvList;

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

            String[] str1 = getResources().getStringArray(R.array.name);
            String[] str2 = getResources().getStringArray(R.array.phone);

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

            lvList.setAdapter(new MyAdapter(str1, str2,
                        CustomListView1Activity.this));
      }
}


2)MyAdapter:



package com.venky.customlistview1;

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

public class MyAdapter extends BaseAdapter {

      public String[] str1, str2;
      public Context context;

      public MyAdapter(String[] str12, String[] str22,
                  CustomListView1Activity customListView1Activity) {
            // TODO Auto-generated constructor stub

            this.str1 = str12;
            this.str2 = str22;
            context = customListView1Activity;
      }

      @Override
      public int getCount() {
            // TODO Auto-generated method stub
            return str1.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 tv1 = (TextView) v.findViewById(R.id.tvName);
            TextView tv2 = (TextView) v.findViewById(R.id.tvPhone);

            tv1.setText(str1[position]);
            tv2.setText(str2[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: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" />

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:text="Name"
            android:textSize="20dp" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:text="Number"
            android:textSize="20dp" />
    </LinearLayout>

    <ListView
        android:id="@+id/lvList"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="@color/myBackgroundColor" >
    </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="horizontal" >

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


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


6)Download this Project Click Here

No comments:

Post a Comment