Search code examples
androidscaleandroid-imageview

How to Make ImageView to Wrap its Contents?


I'm a starter Android learner and I think that this is a very easy question but I can't find it anywhere. I have added an ImageView like this:

//....
            iv = new ImageView(context);
            iv.setBackground(getResources().getDrawable(drawable));
            iv.setAdjustViewBounds(true);

            RelativeLayout.LayoutParams imajpara=new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
            iv.setMaxWidth(100);        
            iv.setMaxHeight(100);
            iv.setLayoutParams(imajpara);           
            iv.setScaleType(ScaleType.CENTER_INSIDE);
//....

But the ImageView (a ball picture in this case) fits the entire screen, even though the image (.png) is 100x100 pixels. In the code you see my attempts to fix this but none of them worked. The imageview is in a RelativeLayout.

So how can I make the ImageView resize itself according to the image's dimensions?


Solution

  • Use setImageDrawable() instead of setBackground().