Quick watch on SwipeRefreshLayout (Tutorial)


After getting update in android support v4, I thought lets check it out. And believe me it is very light and very simple to implement. First of all update Android Support Library from SDK manager. The SwipeRefreshLayout works with Vertical swipe gesture. Lets go through tutorial. Please dont go through coding standards because this is not deep dive ;). 




Create xml with SwipeRefreshLayout.

<android.support.v4.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <ListView
        android:id="@+id/listview"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    </ListView>

</android.support.v4.widget.SwipeRefreshLayout>

Create MainActivity.java

public class MainActivity extends ActionBarActivity {

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);

  if (savedInstanceState == null) {
   getSupportFragmentManager().beginTransaction().add(R.id.container, new PlaceholderFragment()).commit();
  }
 }

 /**
  * A place holder fragment containing a simple view.
  */
 public static class PlaceholderFragment extends Fragment {

  private SwipeRefreshLayout layout;
  String[] values = new String[] { "Android", "iPhone", "Blackberry", "WebOS", "Ubuntu", "Windows7", "Max OS X", "Linux", "OS/2" };
  ArrayList<String> list = new ArrayList<String>();

  public PlaceholderFragment() {
  }

  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
   
   layout = (SwipeRefreshLayout) inflater.inflate(R.layout.fragment_main, container, false);
   final ListView listView = (ListView) layout.findViewById(R.id.listview);
   
   for (int i = 0; i < values.length; i++) {
    list.add(values[i]);
   }
   final ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, list);
   listView.setAdapter(adapter);
   
   layout.setColorScheme(android.R.color.holo_green_dark, android.R.color.holo_orange_dark, android.R.color.holo_blue_dark,
     android.R.color.holo_red_dark);
   layout.setOnRefreshListener(new OnRefreshListener() {

    @Override
    public void onRefresh() {
     Log.e(getClass().getSimpleName(), "refresh");
     for (int i = 0; i < values.length; i++) {
      list.add(values[i]);
     }
     adapter.notifyDataSetInvalidated();
     new GetLinks().execute();
    }
   });
   return layout;
  }

  public class GetLinks extends AsyncTask<Void, Void, Void> {

   @Override
   protected void onPreExecute() {
    super.onPreExecute();

   }

   @Override
   protected Void doInBackground(Void... params) {
    try {
     Thread.sleep(5000);
    } catch (InterruptedException e) {
     e.printStackTrace();
    }
    return null;
   }

   @Override
   protected void onPostExecute(Void result) {
    super.onPostExecute(result);
    layout.setRefreshing(false);
   }
  }

 }

}

Note: This component should be parent view and support only one child.

SourceCode : SwipeRefreshLayout

 

 Reference : SwipeRefreshLayout on devloper site


Mastering ProGuard with ProGuard GUI - Part 1 (ProGuard with JAR)





ProGuard is a nice tool that is used to shrink, obfuscate, and optimize Java code. It is part of Android SDK and very useful when applied correctly. ProGuard provides four main features:
  1. Shrinking
  2. Obfuscation
  3. Repackaging
  4. Optimization
First of all, Download ProGuard from this link : ProGuard. It contains proguardgui.jar in lib folder. Run that jar and ProGuard GUI popup. Follow below steps to apply ProGuard to JAR.

  1. Go to Input/Output:Add JAR file using Add input and specify output folder using Add output.
    New JAR will be placed in Output folder. ProGuard uses it to reconstruct the class dependencies that are necessary for proper processing. The library JAR itself always remain unchanged.

  2. Go to Shrinking:It is necessary to reduce the size of APK. It can be more advantageous because large binaries are less likely to be installed in poor network conditions.
    Shrink collects all unused code and it is possible to list all that unused code to file by providing file path in "Print Usage" section.
  3. Go to Obfuscation:Today, there are plenty of tools available to extract the contents of APKs. It is important to obfuscate to protect the code base. ProGuard generates a mapping file that allows to map the stack traces of obfuscated code to actual code. To get mapping file tick "Print mapping" and give path where file will be dumped."Apply Mapping" is useful to reuse the given name mapping that was printed out in a previous obfuscation run of ProGuard. Classes and class members that are listed in the mapping file receive the names specified along with them.

    "Keep package names" is used to keep the given package name from being obfuscated. Give multiple package name with coma separation.

    "Use mixed-case class names" is useful to generate mixed-case class names while obfuscating.

    "Keep additional class names and class members names" is very important to keep classes from being obfuscated,classes like models, interfaces etc. To keep methods out of Obfuscation, add method in added class with valid access modifiers. For example, if you specify public methods, Public methods would not obfuscate by ProGuard.If method name, return type or argument type is not specify, ProGuard will keep all public methods from obfuscation. 



    "Repackage classes" allows ProGuard to take externals  jar and class files and move them to a single package location. For building libraries, it is very helpful to choose simple interface to third party developers when maintaining well structured project in the repository.

  4. Go to Optimization: It is not recommended to use it because of some dalvik version issues and by default Android tool has optimization turned off in ProGuard.

  5. Go to Information: set target to java version used. Tick verbose to write out more information during process. If execution terminated with exception,  this option will print out the entire stack trace instead of just exception message."Print configuration" is useful to write out the entire configuration that has been parsed, with included files and replaced variables. It will generate file in output folder.

  6. Go to Process: It has an output console for displaying the configuration and the messages while processing. Click on Process and it executes ProGuard with the current configuration. New JAR will be placed in folder which is specified at Input/Output section.

Reference : http://proguard.sourceforge.net/

Get images from Raw folder using only name of Images in Andriod

Sometime we need to get images from raw folder using its name. I came across one scenario where unity developer drop images in raw folder of Android system. Developer returned me only names of that images. And i needed to show dialog of that images using frame animation. There are only three steps to get images from raw folder through its name.

Step - 1: Get resource id of image using its name


int rid = context.getResources()
        .getIdentifier("image_name","raw", context.getPackageName());

Step - 2: Get input stream of image file using that resource id


Resources res = context.getResources();
InputStream in = res.openRawResource(rid);

Step- 3: Get drawable using that input stream


Drawable image = Drawable.createFromStream(in, "image_name"); 

Code snippet :


AnimationDrawable animation = new AnimationDrawable();
for (int count = 0; count < names.length; count++) {
 try {
  int rid = context.getResources().getIdentifier(names[count],
      "raw", context.getPackageName());
  Resources res = context.getResources();
  InputStream in = res.openRawResource(rid);

  byte[] b = new byte[in.available()];
  in.read(b);
  animation.addFrame(Drawable.createFromStream(in, names[count]),
      durationPerFrame);
 } catch (Exception e) {
  e.printStackTrace();
 }
}

animation.setOneShot(false);

Tips for working with Android 4.3 (An Even Sweeter Jelly Bean)


Welcome to Android 4.3, a sweeter version of Jelly Bean! Android 4.3 includes performance optimizations and great new features for users and developers.



I discuss about few things that can be take in care while running your previously published app on Android 4.3.If you have previously published an app for Android, be aware that your app might be affected by changes in Android 4.3. The new feature called Restricted profiles, a new way to manage users and their capabilities on a single device.

There are many cases where your app can misbehave in restricted profile :
  1. If your app uses implicit intents...

    For example, a restricted profile might have the web browser and camera app disabled. So your app should not make assumptions about which apps are available, because if you call startActivity() without verifying whether an app is available to handle theIntent, your app might crash in a restricted profile.When using an implicit intent, you should always verify that an app is available to handle the intent by callingresolveActivity() or queryIntentActivities(). For example:
     
    Intent intent = new Intent(Intent.ACTION_SEND);
    ...
    if (intent.resolveActivity(getPackageManager()) != null) {
        startActivity(intent);
    } else {
        Toast.makeText(context, R.string.app_not_available, Toast.LENGTH_LONG).show();
    }
  2. If your app depends on accounts...
    Any accounts added to the primary user are available to a restricted profile, but the accounts are not accessible from theAccountManager APIs by default.

    Due to these restrictions, you have the following three options:

    • Allow access to the owner’s accounts from a restricted profile.
      To get access to an account from a restricted profile, you must add the android:restrictedAccountType attribute to the <application> tag:
      <application ...
          android:restrictedAccountType="com.example.account.type" >
    • Disable certain functionality when unable to modify accounts.
       If you want to use accounts, but don’t actually require them for your app’s primary functionality, you can check for account availability and disable features when not available.
      UserManager um = (UserManager) context.getSystemService(Context.USER_SERVICE);
      Bundle restrictions = um.getUserRestrictions();
      if (restrictions.getBoolean(UserManager.DISALLOW_MODIFY_ACCOUNTS, false)) {
         // cannot add accounts, disable some functionality
      }

    • Disable your app when unable to access private accounts.
      If it’s instead important that your app not be available to restricted profiles because your app depends on sensitive personal information in an account (and because restricted profiles currently cannot add new accounts), add the android:requiredAccountType attribute to the <application> tag:
      <application ...
          android:requiredAccountType="com.example.account.type" >

  3. Bluetooth Low Energy (Smart Ready)
    You can only scan for Bluetooth LE devices or scan for Classic Bluetooth devices using previous APIs. You cannot scan for both LE and Classic Bluetooth devices at once.

Clustering on Google Map V2 Part-2

We have gone through Clustering on Google map V2 in Clustering on Google Map V2 Part-1 .To overcome drawback of it , I searched for few days and i found one more library which independent from google play service library.

Screenshots :


Features :

  • Clustering based on pixel proximity, not grid membership
  • Animated cluster transitions
  • Supports Android v2.2 (Froyo) and higher

Getting started : 

  • It's easy to add Clusterkraf to your app. Add the Clusterkraf library folder as an Android project to your Eclipse/ADT workspace, and reference it from your app as a library project.
  • Clusterkraf provides an InputPoint class which holds a LatLng position and an Object tag. You just need to construct an ArrayList<InputPoint> object based on your model objects.


    public class YourActivity extends FragmentActivity {
        YourMapPointModel[] yourMapPointModels = new YourMapPointModel[] { new YourMapPointModel(new LatLng(0d, 1d) /* etc */ ) };
        ArrayList inputPoints;
    
        private void buildInputPoints() {
            this.inputPoints = new ArrayList(yourMapPointModels.length);
            for (YourMapPointModel model : this.yourMapPointModels) {
                this.inputPoints.add(new InputPoint(model.latLng, model));
            }
        }
    }
    
  • When your GoogleMap is initialized and your ArrayList<InputPoint> is built, you can then initialize Clusterkraf.
    // YourActivity
    
        Clusterkraf clusterkraf;
    
        private void initClusterkraf() {
            if (this.map != null && this.inputPoints != null && this.inputPoints.size() > 0) {
                com.twotoasters.clusterkraf.Options options = new com.twotoasters.clusterkraf.Options();
                // customize the options before you construct a Clusterkraf instance
                this.clusterkraf = new Clusterkraf(this.map, this.options, this.inputPoints);
            }
        }
    


Advance Options :

  1. To set any type of interpolator in cluster animation :
    options.setTransitionInterpolator(interpolator); 
    /* Types : AccelerateDecelerateInterpolator, AccelerateInterpolator, AnticipateInterpolator,
    AnticipateOvershootInterpolator, BounceInterpolator, DecelerateInterpolator,
    LinearInterpolator, OvershootInterpolator */
    
  2. Clusterkraf calculates whether InputPoint objects should join a cluster based on their pixel proximity. If you want to offer your app on devices with different screen densities, you should identify a Device Independent Pixel measurement and convert it to pixels based on the device's screen density at runtime.
    options.setPixelDistanceToJoinCluster(dipDistanceToJoinCluster);
    
  3. To set zoom to bound animation duration :

    options.setZoomToBoundsAnimationDuration(zoomToBoundsAnimationDuration);
    
  4. To set showInfoWindow animation duration :

    options.setShowInfoWindowAnimationDuration(showInfoWindowAnimationDuration);
    
  5. To set behavior of marker click :

    options.setSinglePointClickBehavior(singlePointClickBehavior);
    /* select from two types SHOW_INFO_WINDOW, NO_OP. */
    
  6. To set behavior of cluster click :

    options.setClusterClickBehavior(this.options.clusterClickBehavior);
    /* select from three types ZOOM_TO_BOUNDS, SHOW_INFO_WINDOW, NO_OP. */
    
  7. To set behavior of cluster info window click :

    options.setClusterInfoWindowClickBehavior(this.options.clusterInfoWindowClickBehavior);
    /* select from three types ZOOM_TO_BOUNDS, HIDE_INFO_WINDOW, NO_OP. */
    
  8. To set padding from window sides after zooming :

    options.setZoomToBoundsPadding(pixels);
    /* When zooming to the bounds of a marker's backing ClusterPoint, zoom until
     * all of the points are at least this far from the edge of the GoogleMap's
     * bounds
     */
    
  9. To set marker click listner :

    options.setOnMarkerClickDownstreamListener(new OnMarkerClickDownstreamListener(new OnMarkerClickDownstreamListener() {
       
       @Override
       public boolean onMarkerClick(Marker marker, ClusterPoint clusterPoint) {
        // TODO Auto-generated method stub
        return false;
       }
      }));
    
  10. To get methods of starting and finishing clustering :

    options.setProcessingListener(this);
    /* generate two methods */
    @Override
     public void onClusteringStarted() {
      
     }
    
     @Override
     public void onClusteringFinished() {
     }

Download Source code : ClusterOnGoogleMapV2


Reference Link: Clusterkraf