Wednesday, January 30, 2013

Expandable ListView Android

Expandable List View is per-define widget in android . and much similar to android List View.

public static void setListViewHeightBasedOnChildren(ListView listView) {
        ListAdapter listAdapter = listView.getAdapter();
        if (listAdapter == null) {
               return;
        }

        int totalHeight = 0;
        int desiredWidth = MeasureSpec.makeMeasureSpec(listView.getWidth(), MeasureSpec.AT_MOST);
        for (int i = 0; i < listAdapter.getCount(); i++) {
            View listItem = listAdapter.getView(i, null, listView);
            listItem.measure(desiredWidth, MeasureSpec.UNSPECIFIED);
            totalHeight += listItem.getMeasuredHeight();
        }

        ViewGroup.LayoutParams params = listView.getLayoutParams();
        params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
        listView.setLayoutParams(params);
        listView.requestLayout();
    }

Check Network Connectivity for Android

Following code for check Net Connectivity on android:


public static boolean IsNetworkAvailable(Context context) {
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);    
         NetworkInfo networkInfo = cm.getActiveNetworkInfo();    
         if (networkInfo != null && networkInfo.isConnected())
         {        
                      return true;    
          }    
  return false;

Zoom Gallary Image Animation On Double Tap

I’m posting here the source code, if anyone is interested, or simply doesn’t want to waste the time creating a new one.

gallery.setOnItemClickListener(new OnItemClickListener() {
    @SuppressWarnings("unused")
    public void onItemClick(AdapterView<?> adapter, View view, int position, long id) {
        count++;
        View lastView = null;
        Animation grow = AnimationUtils.loadAnimation(getBaseContext(), R.anim.image_scale);
        if(count == 1){
            Calendar cal = Calendar.getInstance();
            prvsSec = cal.get(Calendar.SECOND);
        }
        if(count == 2 ){
            Calendar cal = Calendar.getInstance();
            curSec = cal.get(Calendar.SECOND);
            count = 0;
            if((curSec - prvsSec) <=1){
                if(!hasPrvsTap){
                    try {
                        if (lastView != null)
                            lastView.clearAnimation();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    try {
                        view.startAnimation(grow);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    lastView = view;
                    hasPrvsTap = true;
                }
                else{
                    view.clearAnimation();
                    hasPrvsTap = false;
                }
            }
            else{
                Calendar cal2 = Calendar.getInstance();
                prvsSec = cal2.get(Calendar.SECOND);
                count = 1;
            }
        }
    }
});



I get Calender Instance because of calculate time between two tap.

Following are animation for image scale .Place image_scale.xml in res-> anim:

<?xml version="1.0" encoding="utf-8"?>
<scale xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="150"
    android:fillAfter="true"
    android:fromXScale="1.0"
    android:fromYScale="1.0"
    android:interpolator="@android:anim/accelerate_decelerate_interpolator"
    android:pivotX="50%"
    android:pivotY="50%"
    android:toXScale="1.25"
    android:toYScale="1.25" >
</scale>

Location Change on Emulator

How to simulate location change in the emulator?

Start the Emulator from eclipse

Start a command prompt

Type - telnet localhost 5554

This connects the telnet client to the android emulator (assuming emulator is running locally listening on port 5554. Else this connection fails)
Now type
geo fix 79.000000 13.000000 (or your latitude and longitude)

This sends the location change signal to the emulator.

If telnet doesn't works then go to Control Panel\Programs then turn windows feature on or off check telnet client.