Lalith Mohan
Lalith Mohan

Reputation: 3906

Android: Set minimum height of a view programmatically

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

Upvotes: 25

Views: 35716

Answers (2)

FutureJJ
FutureJJ

Reputation: 2688

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).

Upvotes: 27

AKD
AKD

Reputation: 3966

try this--->

siv.getLayoutParams().height = minHeight;

or u can also try--->

LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(width , minHeight);
siv.setLayoutParams(layoutParams);

Upvotes: 11

Related Questions