1

Screen:

Good afternoon to everyone. I want to set the image to a ImageView size relative to the parent container as a percentage. But the size of the picture does not change.

ConstraintLayout constraintLayout = new ConstraintLayout(this);
constraintLayout.setId(View.generateViewId());

ConstraintSet set = new ConstraintSet();

ImageView contactUs = new ImageView(this);
contactUs.setImageResource(R.drawable.rezgoriz);
contactUs.setId(View.generateViewId());
constraintLayout.addView(contactUs, 0);

set.clone(constraintLayout);

set.constrainPercentHeight(constraintLayout.getId(), 20);
set.constrainPercentWidth(constraintLayout.getId(), 20);

set.applyTo(constraintLayout);

setContentView(constraintLayout);

I'd appreciate any help. If anyone knows a publication in which is very detailed how to program in ConstraintLayout with good examples, I will also be grateful.

Hakan Dilek
  • 2,178
  • 2
  • 23
  • 35
Pvl Pa
  • 11
  • 1

1 Answers1

0

You want the ImageView to be a percentage of the ConstraintLayout's height and width, so you need to target the ImageView (contactus) and not the ConstraintLayout. The value set also needs to be a float of 20% or 0.2f:

set.constrainPercentHeight(contactUs.getId(), 0.2f);
set.constrainPercentWidth(contactUs.getId(), 0.2f);
Cheticamp
  • 61,413
  • 10
  • 78
  • 131
  • Cheticamp, https://developer.android.com/reference/androidx/constraintlayout/widget/ConstraintSet, Sets the height of the view as a percentage of the parent. – Pvl Pa Jul 09 '20 at 20:21
  • @PvlPa That is correct, but which is the child view and which is the parent? I say the _ImageView_ is the child view and the _ConstraintLayout_ is the parent, so it is `set.constrainPercentHeight(contactUs.getId(), 0.2f)` and not what you have. Am I missing something here? – Cheticamp Jul 09 '20 at 21:48
  • your size is taken from a child view. – Pvl Pa Jul 10 '20 at 10:33