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?