Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programmatically change height in constraint layout?

I have a simple layout: a fixed height TextView with three ImageView objects anchored to the bottom of the view.

enter image description here

How can I programmatically change the height of each of the ImageViews so they vary within some range (10dp, 16dp below the TextView)? Concretely, my activity gets a list of percentages (between 0 and 1) representing how much of that vertical space each of those "bars" should take up.

I've tried the following without success:

// No visible difference
imgView.setMinHeight(newHeight);

// No change to height, but horizontal constrained was messed up
imgView.setLayoutParams(new LayoutParam(imgView.getWidth(), newHeight);
like image 555
rodrigo-silveira Avatar asked Oct 05 '17 14:10

rodrigo-silveira


1 Answers

If by "height" you mean the absolute top-to-bottom dimension of the ImageView, you can do the following to double the height of an ImageView. You can set the height to the value that you want.

ImageView iv = (ImageView) findViewById(R.id.imageView);
ConstraintLayout.LayoutParams lp = (ConstraintLayout.LayoutParams) iv.getLayoutParams();
lp.height = lp.height * 2;
iv.setLayoutParams(lp);

See ConstraintLayout.LayoutParams.

But, if by "height" you mean the vertical position of the ImageView, you can do the following to change the ImageView's vertical bias (assuming that you are using ConstraintLaytout):

ConstraintSet cs = new ConstraintSet();
ConstraintLayout layout = (ConstraintLayout) findViewById(R.id.layout);
cs.clone(layout);
cs.setVerticalBias(R.id.imageView, 0.25f);
cs.applyTo(layout);

See setVerticalBias.

There are other ways to move an ImageView about the layout, but which you use would depend on how your layout is set up.

like image 51
Cheticamp Avatar answered Oct 21 '22 22:10

Cheticamp