set height of imageview as matchparent programmatically

AndroidHeightImageview

Android Problem Overview


I need to set the height of an imageview as matchparent programatically.if it is a fixed height i know how to set.

but how can i set it as matchparent?

EDIT:

actually height of the parent layout is dynamic.so i need to make the height of the imageview as the height of parent.

Android Solutions


Solution 1 - Android

imageView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));

Solution 2 - Android

imageView.getLayoutParams().height= ViewGroup.LayoutParams.MATCH_PARENT;

Solution 3 - Android

imageView.setLayoutParams
    (new ViewGroup.MarginLayoutParams
        (width, ViewGroup.LayoutParams.MATCH_PARENT));

The Type of layout params depends on the parent view group. If you put the wrong one it will cause exception.

Solution 4 - Android

For Kotlin Users

val params = mImageView?.layoutParams as FrameLayout.LayoutParams
params.width = FrameLayout.LayoutParams.MATCH_PARENT
params.height = FrameLayout.LayoutParams.MATCH_PARENT
mImageView?.layoutParams = params

Here I used FrameLayout.LayoutParams since my views( ImageView) parent is FrameLayout

Solution 5 - Android

I had same issue. Resolved by firstly setting :

imageView.setMinHeight(0);
imageView.setMinimumHeight(0);

And then :

imageView.getLayoutParams().height= ViewGroup.LayoutParams.MATCH_PARENT;

setMinHeight is defined by ImageView, while setMinimumHeight is defined by View. According to the docs, the greater of the two values is used, so both must be set.

Solution 6 - Android

You can try this incase you would like to match parent. The dimensions arrangement is width and height inorder

web = new WebView(this);
        web.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));

Solution 7 - Android

You can use the MATCH_PARENT constant or its numeric value -1.

Solution 8 - Android

initiate LayoutParams .

assign the parent's width and height and pass it to setLayoutParams method of the imageview

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionandro-girlView Question on Stackoverflow
Solution 1 - AndroidC. LeungView Answer on Stackoverflow
Solution 2 - AndroidLuke AllisonView Answer on Stackoverflow
Solution 3 - AndroidxandyView Answer on Stackoverflow
Solution 4 - AndroidMuhamed Riyas MView Answer on Stackoverflow
Solution 5 - AndroidfisioView Answer on Stackoverflow
Solution 6 - Androiduser3824154View Answer on Stackoverflow
Solution 7 - Androiduser1340450View Answer on Stackoverflow
Solution 8 - AndroidluckylukeView Answer on Stackoverflow