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

Sunday 20 May 2012

Hide div when clicking outside the div Or anywhere on the page

Android blog with example
Hello Friends,
Yesterday while creating a web application in Java ,Jsp and Jquery I trapped
in a really weird situation. I make a div which show and hide on clicking
the down arrow icon as shown in the image.


My code is this:
1. JSP CODE:
     <div class="header">
            <div id="logo">
                <div id="emailAddress">
                    <a id="home" href="home.html">
                         mukesh@gmail.com
                    </a>
                    <div id="showSettingBox" class="dwnArrow ">&nbsp;
                    </div>
                    <div class="settingBox">
                        <a href="editProfile.html">Profile</a>
                        <a href="#.">Setting</a>
                        <a href="main.html">Logout</a>
                    </div>
                </div>
            </div>
        </div>
2. Java Script | JQuery Code:
     $("#showSettingBox").click(function(){
                    if($("#showSettingBox").hasClass("showBox"))
                    {
                        $("#showSettingBox").removeClass("showBox");
                        $(".settingBox").hide();
                    }
                    else
                    {
                        $("#showSettingBox").addClass("showBox");
                        $(".settingBox").show();
                    }
                }); 
    
But the div doesn't hide when we are clicking outside it or anywhere on the Page.
As shown in the below image.



Solution:
 If anyone else has this problem, here's a quick solution.  

  $("#showSettingBox").click(function(){
                    if($("#showSettingBox").hasClass("showBox"))
                    {
                        $("#showSettingBox").removeClass("showBox");
                        $(".settingBox").hide();
                    }
                    else
                    {
                        $("#showSettingBox").addClass("showBox");
                        $(".settingBox").show();
                    }
                }); 
                $('html').click(function() {
                //Hide the menus if visible
                $("#showSettingBox").removeClass("showBox");
                              $(".settingBox").hide();
                });

                $('#showSettingBox').click(function(event){
                    event.stopPropagation();
                })

I added the above selected(5-6 line) code in my java script code and its 
working for me. :)
Just Use event.stopPropagation(); with the Id of div.

Hope this will helps you !!!
Happy Coding.... :)
Enjoy :)


Friday 11 May 2012

Android(Java) - gson\json deserialization error

Android Blog With example



Hello Friends,
 
     I found a strange issue in android application , My application is running 
     Fine but in few of the device(like:HTC Explorer, HTC Desire.....) its gives  
     parsing error.....
            
           JsonParseException: The JsonDeserializer com.google.gson.  
           DefaultTypeAdapters$CollectionTypeAdapter@4e76fba0 failed to
            deserialize ...
                
   After doing a lot of investigation  , Here are the steps which I follow to fixed 
   this issue:
    1) Download jarjar (http://code.google.com/p/jarjar/downloads/list)
    2) Put jarjar-1.3.jar and gson-2.1.jar in the same folder
    3) Create a new text file in this folder (rules.txt)
    4) Write the following line in the textfile:     
         rule com.google.gson.** com.google.myjson.@1
 
   5) From the command line, go in to the folder where you placed 
        jarjar-1.3.jar and gson-2.1.jar and run the following 
        command:
      "java -jar jarjar.jar process rules.txt gson-2.1.jar myjson-2.1.jar"  
 
  6) Replace the gson library in your project with myjson and
      update the imports.
  
  Hope this will helps you!
  cheers! :)
  Happy coding....... 
 



 

Copyright @ 2013 Android Developers Blog.