0

Hi I wanted to change height and width property of image view inside my activity I tried it in following way but its not working for me ...

View card_view = getLayoutInflater().inflate(R.layout.card_details1,null);
coupon_img = (ImageView) card_view.findViewById(R.id.coupon_image);
// I tried this ////////
coupon_img.getLayoutParams().height = 20;
coupon_img.getLayoutParams().width = 20;
// I also tried this ////
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(100, 100);
    coupon_img.setLayoutParams(layoutParams);
// also this one ////
coupon_img.setMaxHeight(10);

But I am not able to change height and width of imageview src. Is there any mistake I am doing? How to do this? Need help... Thank you...

nilkash
  • 7,408
  • 32
  • 99
  • 176

3 Answers3

4

In this piece of code, I am creating a new instance of an ImageView at runtime and setting dimensions to it:

// SET THE IMAGEVIEW DIMENSIONS
int dimens = 120;
float density = activity.getResources().getDisplayMetrics().density;
int finalDimens = (int)(dimens * density);

LinearLayout.LayoutParams imgvwDimens = 
        new LinearLayout.LayoutParams(finalDimens, finalDimens);
imgAlbumPhoto.setLayoutParams(imgvwDimens);

// SET SCALETYPE
imgAlbumPhoto.setScaleType(ScaleType.CENTER_CROP);

// SET THE MARGIN
int dimensMargin = 5;
float densityMargin = activity.getResources().getDisplayMetrics().density;
int finalDimensMargin = (int)(dimensMargin * densityMargin);

LinearLayout.LayoutParams imgvwMargin = 
        new LinearLayout.LayoutParams(finalDimens, finalDimens);
imgvwMargin.setMargins
(finalDimensMargin, finalDimensMargin, finalDimensMargin, finalDimensMargin);

This will set the dimensions of the ImageView. They will, however, be in px. Use the code from here if you need dp values: https://stackoverflow.com/a/9563438/450534

UPDATED:

To change the dimensions of an existing ImageView that has already been defined in the XML, use this:

coupon_img.setLayoutParams(new LayoutParams(100, 100));
Community
  • 1
  • 1
Siddharth Lele
  • 27,623
  • 15
  • 98
  • 151
  • Thank you Siddharth for you r help. Can you please tell how to change image height and width if I already defined it into xml file. – nilkash Feb 21 '13 at 07:00
  • @nilkash: hmm.. let me get back to the office, test a few things and update. – Siddharth Lele Feb 21 '13 at 07:35
  • @nilkash: Is there a reason why you are using the ImageView from an inflated layout? I am asking for me do test it within your current parameters. Also, what is the default width and height set for the ImageView in the XML? – Siddharth Lele Feb 21 '13 at 08:41
  • @sidhharth thank you for reply.. My actual problem is that when i am testing my application on device which having dimension like 720*1270 (but for android xhdp it is 960dp x 720dp) then its not looking good. that's the reason i wanted changes my images size according to screen resolution. – nilkash Feb 21 '13 at 08:48
  • @nilkash: Then is any of what I suggested working at the moment? – Siddharth Lele Feb 21 '13 at 08:57
2

try some thing like this...

 LayoutParams params = new LayoutParams(100, 100);
 parantlayout.addView(coupon_img, params);

I think this will help you.

Raj
  • 1,843
  • 2
  • 21
  • 47
2

I think you are not adding the changed image to the layout..

LinearLayout ll = (LinearLayout)findViewById(R.layout.yourlinearlayout);

image.setLayoutParams(
          new LinearLayout.LayoutParams(
                 bmp.getWidth(), 
                 bmp.getHeight())); 
ll.addView(image);// Then add the image to linear layout
Ram kiran Pachigolla
  • 20,897
  • 15
  • 57
  • 78