Search code examples
androidandroid-imageviewandroid-image

How to change imagesize dynamically


I have defined imageview size in xml file. So, it will takes it as default when image appear. I'm displaying different images based on some condition. So, i need to change the image widht and height.

xml:

<ImageView
        android:id="@+id/imageView1"
        android:layout_width="100dp"
        android:layout_height="100dp"        
        android:visibility="invisible"  />

Java:

if (text.equalsIgnoreCase("y")) {                      
      image.setImageResource(R.drawable.y); 

      }                    
    else if (text.equalsIgnoreCase("z")) {
    image.setImageResource(R.drawable.z); // Here i need to change the width & height of the image
               }

Solution

  • You can do it this way:

    imgView.getLayoutParams().height = 300;
    imgView.getLayoutParams().width= 300;
    imgView.requestLayout();//changing width and height programmatically will require the view to be redrawn