Setting a maximum width on a ViewGroup

One option which is what I did is to extend LinearLayout and override the onMeasure function. For example: public class BoundedLinearLayout extends LinearLayout { private final int mBoundedWidth; private final int mBoundedHeight; public BoundedLinearLayout(Context context) { super(context); mBoundedWidth = 0; mBoundedHeight = 0; } public BoundedLinearLayout(Context context, AttributeSet attrs) { super(context, attrs); TypedArray a = … Read more

Does setWidth(int pixels) use dip or px?

It uses pixels, but I’m sure you’re wondering how to use dips instead. The answer is in TypedValue.applyDimension(). Here’s an example of how to convert dips to px in code: // Converts 14 dip into its equivalent px Resources r = getResources(); int px = Math.round(TypedValue.applyDimension( TypedValue.COMPLEX_UNIT_DIP, 14,r.getDisplayMetrics()));