monty.py
monty.py

Reputation: 2799

Best practice for helper methods in android

I have two custom views which both inherits from RelativeLayout. Both need to have same helper methods.

f.e.

public static void setViewHeight(View v, int heightInDp) {
    ViewGroup.LayoutParams layoutParams = v.getLayoutParams();
    layoutParams.height = Helper.convertDpToPixel(heightInDp);

    v.setLayoutParams(layoutParams);
}

As you can see, currently I'm using a helper/ utility class with static methods for this purpose. But I'm feeling uncomfortable with it and searching for a cleaner way, not to pass Views to a static context.

Another idea is to write an abstract base class which extends from RelativeLayout. But I don't want to be bounded to RelativeLayout if I want to use the helper methods in other custom views later.

My last idea is to create a class for each Helper method. For the example above it could be sth like this:

public class LayoutTransition {

    private View mView;

    public LayoutTransition(View v) {
        mView = v;
    }

    public View withHeight(int height) {
        ViewGroup.LayoutParams layoutParams = mView.getLayoutParams();
        layoutParams.height = height;
        mView.setLayoutParams(layoutParams);
        return mView;
    }

}

But here I have to always use copies and a lot of objects.

What is the best practice for this difficulty?

Upvotes: 2

Views: 1763

Answers (2)

user4774371
user4774371

Reputation:

I have two custom views which both inherits from RelativeLayout.

If you have only two custom views then define methods separately in each view's class. I know it will add redundancy, but it is better than passing view to static method or creating many objects.

It is better to create static methods only if objects passed to method are immutable (final), in best case scenario primitive datatypes. Otherwise avoid static helper methods if possible.

In your case separate simple java classes like LayoutTransition seems good solution if you have more than two custom views.

What is the best practice for this difficulty?

It always depend on situation, so there isn't any perfect solution. There will always be trade-offs

Upvotes: 1

Alex Townsend
Alex Townsend

Reputation: 1564

I generally use a final class with a private constructor to create a util class for something like this, and pass in a reference to the Context to the util class. That way your helper/util class is not holding on to any references to the Context, example below:

public final class LayoutUtils {

  private LayoutUtils() {
    throw new IllegalStateException("No instances.");
  }

  public static void convertDpToPixel(final int heightInDp, final Context context) {
    // make use of final to protect your context
    // do stuff w/ context and height here
  }
}

And you would just be able to use this in code as LayoutUtils.convertDpToPixel(height, context).

This pattern is easily to use with things like a View or Context.

Upvotes: 3

Related Questions