1

I have created an ImageView by code and I want it to have a max size of 24x24dp. But the setMaxHeight/Width aren't working. The image I use is a svg Drawable and it's size is 40x40dp.

I have also tried to use imageView.setMinimumHeight/Width and

imageView.setLayoutParams(new ViewGroup.LayoutParams((int) getTypedValueInDP(24), (int) getTypedValueInDP(24)));

without any effect!

Here I am creating the ImageView:

        ImageView imageView = new ImageView(context);
        imageView.setMaxHeight(getTypedValueInDP(20));
        imageView.setMaxWidth(getTypedValueInDP(20));
        imageView.setImageDrawable(context.getResources().getDrawable(drawable));
        imageView.setPadding(left, 0, left, 0);

and I'm using it with a RelativeLayout also made in code like this:

    RelativeLayout toastLayout = new RelativeLayout(context);
    toastLayout.setBackground(getBLLShape());
    toastLayout.addView(getImageView());
blizzard
  • 5,275
  • 2
  • 34
  • 48

3 Answers3

0

You need to specify the LayoutParams for the ImageView before adding to the ViewGroup.

ImageView imageView = new ImageView(this);
imageView.setMaxHeight(getTypedValueInDP(20));
imageView.setMaxWidth(getTypedValueInDP(20));
imageView.setAdjustViewBounds(true);
imageView.setImageDrawable(context.getResources().getDrawable(drawable));
RelativeLayout.LayoutParams layoutParams =
    new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
        RelativeLayout.LayoutParams.WRAP_CONTENT);
imageView.setLayoutParams(layoutParams);

RelativeLayout toastLayout = new RelativeLayout(this);
toastLayout.addView(imageView);
blizzard
  • 5,275
  • 2
  • 34
  • 48
  • It worked. But I also tried to just put `imageView.setAdjustViewBounds(true);` and remove any code related to `LayoutParams` and It still worked –  Jan 25 '17 at 11:37
  • @Muddz Cool. I added `setAdjustViewBounds` based on this [post](http://stackoverflow.com/a/9303270/549978). – blizzard Jan 25 '17 at 11:39
  • There something wrong. If `imageView.setMaxWidth(getTypedValueInDP(20));` it causes the parent layout which is a `RelativeLayout` to have a height long as the screen and the `ImageView` disappears! But with out maxWidth it behaves as it should excecpt maxWidth isn't going to be set See this sceeenshot: http://i.imgur.com/lFJoU2C.png –  Jan 25 '17 at 12:05
  • @Muddz Please post the screenshot of how it looks without maxWidth. – berserk Jan 25 '17 at 16:14
  • @berserk **With: setMaxWidth();**: http://i.imgur.com/lFJoU2C.png **Without setMaxWidth();**: http://i.imgur.com/qIyddAP.png –  Jan 25 '17 at 16:41
  • @Muddz I don't know what are you talking about, With `setAdjustViewBounds(true)` the image is drawn to the defined size. Also check by commenting the `setBackground(getBLLShape())` part. If it doesn't solve the issue update the post with relevant code. – blizzard Jan 25 '17 at 19:17
  • 1
    @blizzard it has just been solved with another answer. it was the padding that caused the maxWidth to go crazy –  Jan 25 '17 at 20:20
0

The padding seems to be messing up with the setMaxWidth(). Make sure that the padding you give at both sides must not be equal to or greater than setMaxWidth argument. In your code, you are giving padding to left and right side. So, change:

imageView.setMaxWidth(getTypedValueInDP(20));

to

imageView.setMaxWidth(2* left + getTypedValueInDP(20));
berserk
  • 2,690
  • 3
  • 32
  • 63
-1

If your image and imageview have same aspect ratio. Did you try

imageview.setScaleType(ImageView.ScaleType.CENTER_INSIDE)

Muhammed GÜNEŞ
  • 304
  • 2
  • 15
  • I can't ensure that It has since the user has the ability to change the image in the imageview –  Jan 25 '17 at 11:01