1

I have created a new ImageView by Java code, but when I try to set height using setMaxHeight, its not working. Can someone help me to change the height of that ImageView?

ImageView i = new ImageView(MainActivity.this);
i.setImageResource(R.drawable.red);
i.setMaxWidth(50);
i.setMaxHeight(50);
ll.addView(i);
Kaushik
  • 6,077
  • 5
  • 35
  • 53
Jeff Liu
  • 89
  • 1
  • 1
  • 7

4 Answers4

1

Try this:

 i.getLayoutParams().height = 20;
Jaymin Panchal
  • 2,752
  • 2
  • 26
  • 30
  • 2
    all credit goes to owner http://stackoverflow.com/questions/3144940/set-imageview-width-and-height-programmatically – W4R10CK Jan 09 '17 at 06:17
1

The easy way is to set the size programatically like that :

i.setLayoutParams(new LayoutParams(width, height));

if you want to change dp to pixel you can use this Code:

public static float pxFromDp(final Context context, final float dp) {
    return dp * context.getResources().getDisplayMetrics().density;
}

i.setLayoutParams(new LayoutParams(pxFromDp(this, 50), pxFromDp(this, 50)));
Anjal Saneen
  • 2,928
  • 21
  • 38
0
your_image_view.getLayoutParams().width = 100;**
your_image_view.getLayoutParams().height = 100;**

you found your answer Here

Hope this will helps you bro

Community
  • 1
  • 1
Sagar Aghara
  • 630
  • 7
  • 19
0

Use this code to set width and height.

ImageView iv = new ImageView(context);
int width = 60;
int height = 60;
LinearLayout.LayoutParams parms = new LinearLayout.LayoutParams(width,height);
iv.setLayoutParams(parms);
Maya Mohite
  • 593
  • 5
  • 8