Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 985
Lalith Mohan Avatar asked May 28 '13 11:05

Lalith Mohan


People also ask

What are two different ways to set the height and width of component in Android?

First get layout params using view. getLayoutParams(). Second then set the height and width value then last set the layout params of view.


2 Answers

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

like image 82
FutureJJ Avatar answered Sep 28 '22 08:09

FutureJJ


try this--->

siv.getLayoutParams().height = minHeight;

or u can also try--->

LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(width , minHeight);
siv.setLayoutParams(layoutParams);
like image 23
Ashok Damani Avatar answered Sep 28 '22 08:09

Ashok Damani