Writing a class for common block of code used in many other classes In this I have written the setAnalyticsInfo() method.  The code inside this is common to almost all other (30) classes, so I have created GoogleAnalyticsCode and defined one common method and calling this method from all other classes.

import com.google.analytics.tracking.android.EasyTracker;
import com.google.analytics.tracking.android.MapBuilder;

import android.app.Activity;

public class GoogleAnalyticsCode {
    private String caption;
    private Activity activity;
    public GoogleAnalyticsCode(String caption, Activity activity) {
        super();
        this.caption = caption;
        this.activity = activity;
    }

    public void setAnalyticsInfo(){
        EasyTracker easyTracker = EasyTracker.getInstance(activity);

        if(easyTracker!=null)
        {
            easyTracker.send(MapBuilder
                    .createEvent(caption,     // Event category (required)
                            caption,  // Event action (required)
                            caption,   // Event label
                            null)            // Event value
                            .build());
        }
    }
}


I am calling setAnalyticsInfo() method like this in other classes in NavDrawer.java:

      public class NavDrawer {
    public void navigate_to_rating() {
        GoogleAnalyticsCode gac = new GoogleAnalyticsCode("Rate Us", activity);
        gac.setAnalyticsInfo();

        final String appPackageName =activity.getPackageName(); // getPackageName() from Context or Activity object
        try {
            activity.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + appPackageName)));
        } catch (android.content.ActivityNotFoundException anfe) {
            activity.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://play.google.com/store/apps/details?id=" + appPackageName)));
        }
    }
    }


My question is:

Is it good programming standard to write separate classes for common blocks of code used in many other classes?
Is it good programming standard to write separate classes for common blocks of code used in many other classes?


Yes, it is. Not repeating code over and over again is an important principle of programming.

The alternative to creating a separate class would be to create a Base activity which contains this method (in cases that you feel a separate class would be overkill). You could even call this method in the onCreate method of the Base activity, thus automatically using it each time.

In this case it's really a matter of taste. I would probably put it in a method in the Base activity, because your current GoogleAnalyticsCode class doesn't really contain any logic. It just calls EasyTracker:send().

Coding Style

Just a quick side-point concerning your coding style: You should try to be consistent with the position of your opening curly bracket, your indentation, and your spacing.