I'm trying to set minimum height for an imageview programmatically but had no luck so far. My code is below
int width = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 160, getResources().getDisplayMetrics());
int minHeight = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 100, getResources().getDisplayMetrics());
LayoutParams params = new LayoutParams(width,LayoutParams.MATCH_PARENT);
SmartImageView siv = new SmartImageView(getActivity());
siv.setMinimumHeight(minHeight);
siv.setLayoutParams(sivParams);
siv.setAdjustViewBounds(true);
siv.setScaleType(ScaleType.FIT_XY);
siv.setImageUrl(url);
ll.addView(siv);
I also tried to put the setMinimumWidth method after the setLayoutParams method but that didn't worked either. Please help
Thanks
First get layout params using view. getLayoutParams(). Second then set the height and width value then last set the layout params of view.
The answer by @Ashok Damani is incorrect as pointed out by @Gustavo Baiocchi Costa in the comments:
this sets the height or width not the minimum width or height
The correct method to set the minimum height of a view in Android:
Property access way(in Kotlin):
yourView.minimumHeight = minHeight
Setter method way:
yourView.setMinimumHeight(minHeight);
Android Documentation says:
Sets the minimum height of the view. It is not guaranteed the view will be able to achieve this minimum height (for example, if its parent layout constrains it with less available height).
try this--->
siv.getLayoutParams().height = minHeight;
or u can also try--->
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(width , minHeight);
siv.setLayoutParams(layoutParams);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With