How to resize a custom view programmatically? How to resize a custom view programmatically? android android

How to resize a custom view programmatically?


Android throws an exception if you fail to pass the height or width of a view.Instead of creating a new LayoutParams object, use the original one, so that all other set parameters are kept. Note that the type of LayoutParams returned by getLayoutParams is that of the parent layout, not the view you are resizing.

RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) someLayout.getLayoutParams();params.height = 130;someLayout.setLayoutParams(params);


this.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, theSizeIWant));

Problem solved!

NOTE: Be sure to use the parent Layout's LayoutParams. Mine is LinearLayout.LayoutParams!


This works for me:

ViewGroup.LayoutParams params = layout.getLayoutParams();params.height = customHeight;layout.requestLayout();