Tuesday 5 June 2012

Android Alphabetical Indexer Demo |Android Alphabetindexer example



Hello Friends,

Today I am going to share a sample application in Android which have similar
functionality like Iphone Alphabatical Indexing.
I hope you will enjoy with this application . :)

These are the screen shot of this application:

1. Screen shot 1 : showing all places on clicking of all from the left menu

alphabaticalindexer


2. Screen shot 2: Index wise sorting, here i am showing all the item with Index "A" ,"B"
                             and "I"

alphabetical indexer android


alphabetical indexer

Here are sample code of this application:

1. AndroidManifest.xml

 

1:  <?xml version="1.0" encoding="utf-8"?>  
2:     <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
3:    package="com.mukesh.aphabaticalview"  
4:    android:versionCode="1"  
5:    android:versionName="1.0" >  
6:    <uses-sdk android:minSdkVersion="8" />  
7:    <application  
8:      android:debuggable="true"  
9:      android:icon="@drawable/ic_launcher"  
10:      android:label="@string/app_name" >  
11:      <activity  
12:        android:name="com.mukesh.aphabaticalview.Alphbatical"  
13:        android:label="@string/app_name" >  
14:        <intent-filter>  
15:          <action android:name="android.intent.action.MAIN" />  
16:          <category android:name="android.intent.category.LAUNCHER" />  
17:        </intent-filter>  
18:      </activity>    
19:     </application>  
20:       </manifest>  


2. Alphabatical.java (Activity class)


1:  package com.mukesh.aphabaticalview;  
2:  import java.util.ArrayList;  
3:  import android.app.Activity;  
4:  import android.content.Intent;  
5:  import android.os.Bundle;  
6:  import android.view.View;  
7:  import android.widget.AdapterView;  
8:  import android.widget.ArrayAdapter;  
9:  import android.widget.ListView;  
10:  import android.widget.AdapterView.OnItemClickListener;  
11:  public class Alphbatical extends Activity {  
12:   private ListView countryList;  
13:   String item;  
14:   AllDataAdapter adapter;  
15:   int iconName;  
16:   ArrayList<Data> listData = new ArrayList<Data>();  
17:   ArrayList<Data> allPlaceData = new ArrayList<Data>();  
18:   ArrayList<Data> temp = new ArrayList<Data>();  
19:   @Override  
20:   public void onCreate(Bundle savedInstanceState) {  
21:   super.onCreate(savedInstanceState);  
22:   setContentView(R.layout.home);  
23:   final String[] alphabaticalList = { "All", "A", "B", "C", "D", "E",  
24:    "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q",  
25:    "R", "S", "T", "U", "V", "W", "X", "Y", "Z" };  
26:   // side list  
27:   final ListView sideList = (ListView) findViewById(R.id.sideIndex);  
28:   ArrayAdapter<String> alphabaticalListAdapter = new ArrayAdapter<String>(  
29:    this, android.R.layout.simple_list_item_1, alphabaticalList);  
30:   sideList.setAdapter(alphabaticalListAdapter);  
31:   // fill hard coded in array list  
32:   allPlaceData.add(new Data("Afghanistan"));  
33:   allPlaceData.add(new Data("Albania"));  
34:   allPlaceData.add(new Data("Argentina"));  
35:   allPlaceData.add(new Data("Australia"));  
36:   allPlaceData.add(new Data("Austria"));  
37:   allPlaceData.add(new Data("Bangladesh"));  
38:   allPlaceData.add(new Data("Belgium"));  
39:   allPlaceData.add(new Data("Brazil"));  
40:   allPlaceData.add(new Data("China"));  
41:   allPlaceData.add(new Data("Colombia"));  
42:   allPlaceData.add(new Data("Canada"));  
43:   allPlaceData.add(new Data("Denmark"));  
44:   allPlaceData.add(new Data("Dominican"));  
45:   allPlaceData.add(new Data("Egypt"));  
46:   allPlaceData.add(new Data("Ethiopia"));  
47:   allPlaceData.add(new Data("Fiji"));  
48:   allPlaceData.add(new Data("France"));  
49:   allPlaceData.add(new Data("Georgia"));  
50:   allPlaceData.add(new Data("Germany"));  
51:   allPlaceData.add(new Data("Greece"));  
52:   allPlaceData.add(new Data("Hong Kong"));  
53:   allPlaceData.add(new Data("Hungary"));  
54:   allPlaceData.add(new Data("Iceland"));  
55:   allPlaceData.add(new Data("India"));  
56:   allPlaceData.add(new Data("Indonesia"));  
57:   allPlaceData.add(new Data("Iran"));  
58:   allPlaceData.add(new Data("Iraq"));  
59:   allPlaceData.add(new Data("Ireland"));  
60:   allPlaceData.add(new Data("Italy"));  
61:   allPlaceData.add(new Data("Jamaica"));  
62:   allPlaceData.add(new Data("Japan"));  
63:   allPlaceData.add(new Data("Kenya"));  
64:   allPlaceData.add(new Data("Korea, North"));  
65:   allPlaceData.add(new Data("Kuwait"));  
66:   allPlaceData.add(new Data("Lebanon"));  
67:   allPlaceData.add(new Data("Madagascar"));  
68:   allPlaceData.add(new Data("Malaysia"));  
69:   allPlaceData.add(new Data("Mexico"));  
70:   allPlaceData.add(new Data("Nepal"));  
71:   allPlaceData.add(new Data("New Zealand"));  
72:   allPlaceData.add(new Data("Oman"));  
73:   allPlaceData.add(new Data("Pakistan"));  
74:   allPlaceData.add(new Data("Poland"));  
75:   allPlaceData.add(new Data("Qatar"));  
76:   allPlaceData.add(new Data("Romania"));  
77:   allPlaceData.add(new Data("Russia"));  
78:   allPlaceData.add(new Data("Spain"));  
79:   allPlaceData.add(new Data("Sri Lanka"));  
80:   allPlaceData.add(new Data("Switzerland"));  
81:   allPlaceData.add(new Data("Taiwan"));  
82:   allPlaceData.add(new Data("Turkey"));  
83:   allPlaceData.add(new Data("Uganda"));  
84:   allPlaceData.add(new Data("United Kingdom"));  
85:   allPlaceData.add(new Data("Vietnam"));  
86:   allPlaceData.add(new Data("Yemen"));  
87:   allPlaceData.add(new Data("Zambia"));  
88:   allPlaceData.add(new Data("Zimbabwe"));  
89:   adapter = new AllDataAdapter(this, R.layout.home_list, allPlaceData);  
90:   countryList = (ListView) findViewById(R.id.list);  
91:   countryList.setAdapter(adapter);  
92:   temp.addAll(allPlaceData);  
93:   /*  
94:    * On Item click listener  
95:    */  
96:   countryList.setOnItemClickListener(new OnItemClickListener() {  
97:    public void onItemClick(AdapterView<?> parent, View v,  
98:     final int position, long id) {  
99:    // TODO Auto-generated method stub  
100:        // show toast  
101:    }  
102:   });  
103:   /*  
104:    * On click of alphabatical list view item  
105:    */  
106:   sideList.setOnItemClickListener(new OnItemClickListener() {  
107:    public void onItemClick(AdapterView<?> parent, View v,  
108:     int position, long id) {  
109:      item = (String) sideList.getAdapter().getItem(position);  
110:      listData.clear();  
111:      for (int i = 0; i < temp.size(); i++) {  
112:       String name = temp.get(i).title;  
113:       String subName = name.substring(0, 1);  
114:        if (subName.startsWith(item)) {  
115:            listData.add(new Data(name));  
116:         }  
117:     // for all click.........  
118:     if (position == 0) {  
119:     listData.add(new Data(name));  
120:     }  
121:    }  
122:    allPlaceData.clear();  
123:    allPlaceData.addAll(listData);  
124:    AllDataAdapter adapter = new AllDataAdapter(Alphbatical.this,  
125:    R.layout.home_list, allPlaceData);  
126:    countryList.setAdapter(adapter);  
127:    }  
128:   });  
129:   }  
130:  }  


AlphabaticalIndex.zip

Other Blogs Related to this:
   1. Horizontal Listview in android
   2. Custom ListView 
   3. Custom ListView with search

Mukesh Kumar

Hi Guys I am from Delhi working as Web/Mobile Application Developer(Android Developer), also have knowledge of Roboelctric and Mockito ,android test driven development... Blogging has been my passion and I think blogging is one of the powerful medium to share knowledge and ideas....

9 comments:

  1. Very nice it worked for me but only proble i m facing now is i want that scroll bar in Horizontal way.

    ReplyDelete
  2. Hey Sourabh..
    You have to make change inside the xml file ,there i am using two listview. You have to put this inside Relative layout and make change in second Layout.

    ReplyDelete
  3. Actually i am working on custom list view AraayAdapter extends BaseAdapter implements filterable.now i want alphabetical Indexer.I am using your code.but it is not working.how to solve that problem.



    thankyou sir.

    ReplyDelete
  4. Thanks Krishnarjun,

    Will you please share your code with me , So that i will help you.

    ReplyDelete
  5. Hello Krishnarjun.

    Please check this link , here i am showing the Horizontal listview in android:
    http://mukeshyadav4u.blogspot.in/2012/11/horizontal-listview-in-android-example.html

    ReplyDelete
  6. Ur coding is working pakka... But here data are harcoded, i want to add data at runtime. In my case i have to load the listview data from the database

    ReplyDelete
  7. Hello Rajalakshmi,

    No,worries just fetch your data from in a ArrayList object and the pass this arraylist inside adapter.

    ReplyDelete
  8. hi,
    thnks for ur code it helpd me a lot its working perfectly as i needed....
    thnks mukesh..

    ReplyDelete

 

Copyright @ 2013 Android Developers Blog.