set height of imageview as matchparent programmatically - android

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.

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

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

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.

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

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.

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

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

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

Related

How to set "width" parameter back to "wrap_content" after set it to a specific value in code?

For example, a TextView, whose original "width" in xml is "wrap_content". Then in java code, I set it to a specific value such as 50px.
Now I want to change it back to "wrap_content", by means of changing the "LayoutParameter", but it didn't work.
So what can I do with it? Thank you!
RESOLVED
You can use the WRAP_CONTENT constant defined in ViewGroup.LayoutParams. Try something like this:
TextView tv = // your view here
ViewGroup.LayoutParams params = tv.getLayoutParams();
params.width = ViewGroup.LayoutParams.WRAP_CONTENT;
tv.setLayoutParams(params);

How to change width and height of existing layout params

Please consider ImageView inside RelativeLayout it has some specific layout params for RelativeLayout. I need to change the width and height of my ImageView programatically according to the screen size.
How can I save all layout parameters but only change width and height
Following code creates empty layout params but I need to save my old params and change only height and width.
imageViewArticleImage.setLayoutParams(new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, mScreenSize.y / 3));
How can I achieve this ?
Use getLayoutParams to get the current layout params. Change the width and height and go from there. IF the view is already visible, call requestLayout to force a new layout pass.

Android change size programmatically

I need to change the size of the buttons programmatically.
I read some answers and I do the next to change the button size.
button.setLayoutParams(new RelativeLayout.LayoutParams(150, 50));
This code change the size correctly but the problem is that the button not stay in the position I want, it moves the button to the top left corner.
I need to change the button size but I don't want to change de position.
How can I fix this?
You have to use the existing RelativeLayout Rules as well,i.e whether the item is center aligned,align parent left etc. Or you can make use of the existing Params and modify the width and height as follows the RelativeLayout rules will not be modified.
RelativeLayout.LayoutParams layoutParams= (RelativeLayout.LayoutParams) button.getLayoutParams();
layoutParams.width=150;
layoutParams.height=50;
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) button.getLayoutParams();
params.width = 500;
params.height = 500;
button.setLayoutParams(params);

Center view in android after changing its width properties

I'm changing the width of a grid view based on its column width and number of columns (which works):
gridView.setLayoutParams(new LinearLayout.LayoutParams(gridManipulation.getColumnWidth() * (int)Math.sqrt(str.length) + 10, LayoutParams.FILL_PARENT));
gridView.setGravity(Gravity.CENTER);
I've tried re-centering it in the parent view but its not working, does anyone know why, its still centred as if it still has the width parameters before my dynamic change.
Set the Gravity as LayoutParams property then set that LayoutParams to your GridView as follows...
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(gridManipulation.getColumnWidth() * (int)Math.sqrt(str.length) + 10, LayoutParams.FILL_PARENT);
params.gravity = Gravity.CENTER;
gridView.setLayoutParams(params);
you cant center it if its bigger than its parent, which is never bigger than the screen, unless you set it manually, which will cause the system to render data outside the screen and slow you way down.....
is it expanding verticaly or horizontally or both? my recomendation would be to put it in a scrollview.

Specify Width as 0dp with weight in android

I have textview for which i want to use weight as well as width, but i want to pass width as 0dp, if i set width in layout params as 0, it does not render on screen.
How can i set width as 0dp dynamically through code
I even tried giving width as LayoutParams.Wrap_Content, but that also hasn't worked
LinearLayout.LayoutParams tv_params_level = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT, 0.1f);
If you set the width of the TextView via xml to 0dp you must set then its weight to 1 or other value greater than 0. To set the width programatically:
LinearLayout.LayoutParams tv_params_level = new LinearLayout.LayoutParams(0,
LayoutParams.WRAP_CONTENT, 1.0);
Then set this layout parameteres to your TextView:
tv.setLayoutParams(tv_params_level);

Categories

Resources