set height of imageview as matchparent programmatically
Asked Answered
S

8

108

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.

Semibreve answered 25/4, 2012 at 6:38 Comment(2)
i have tried all the methods mentioned in the answers.but it is not setting the height as match parent :(Semibreve
You should review your accepted answerDellora
B
-19

initiate LayoutParams .

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

Bijouterie answered 25/4, 2012 at 6:51 Comment(2)
you should never pass the parents actual dimensions. Always use MATCH_PARENT instead of actual values. In general I wouldn't recommend using absolute sizes because it might look completely different on all the screens there are. I'd prefer using weights.Sculptor
To be honest this should be a commentCesta
M
291
imageView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
Mutt answered 25/4, 2012 at 6:44 Comment(5)
The moment i press Alt+Enter in LayoutParams, i see so many classes. Can you please tell me which one to use?Shotputter
what is the parent view?Mutt
You should use class of view that You want to set LayoutParams i.e. use LinearLayout.LayoutParams to set prams to Your LL :)Nefertiti
What if you want the height to be dynamic based on the dimensions of the original image but want the width to be MATCH_PARENT?Comedown
import android.view.ViewGroupRennes
S
67
imageView.getLayoutParams().height= ViewGroup.LayoutParams.MATCH_PARENT;
Scathe answered 24/5, 2016 at 10:17 Comment(1)
If you're setting the params for the first time because you made the imageview programmatically, this will null pointer error (or does with text view) and should everywhere else.Maggot
L
17

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

Linstock answered 13/2, 2019 at 11:42 Comment(0)
I
15
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.

Insensibility answered 25/4, 2012 at 6:48 Comment(0)
G
8

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.

Gosplan answered 16/2, 2020 at 14:24 Comment(0)
B
3

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));
Boots answered 9/4, 2016 at 5:38 Comment(0)
C
1

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

Clouet answered 25/4, 2012 at 6:45 Comment(1)
MATCH_PARENT is not defined in the context. You must say 'LayoutParams.MATCH_PARENT'Aquitaine
B
-19

initiate LayoutParams .

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

Bijouterie answered 25/4, 2012 at 6:51 Comment(2)
you should never pass the parents actual dimensions. Always use MATCH_PARENT instead of actual values. In general I wouldn't recommend using absolute sizes because it might look completely different on all the screens there are. I'd prefer using weights.Sculptor
To be honest this should be a commentCesta

© 2022 - 2024 — McMap. All rights reserved.