Android: resizing an ImageView in the XML

191,386

Solution 1

for example:

<ImageView android:id="@+id/image_view"     
  android:layout_width="wrap_content"  
  android:layout_height="wrap_content"  
  android:adjustViewBounds="true"  
  android:maxWidth="42dp"  
  android:maxHeight="42dp"  
  android:scaleType="fitCenter"  
  android:layout_marginLeft="3dp"  
  android:src="@drawable/icon"  
  /> 

Add property android:scaleType="fitCenter" and android:adjustViewBounds="true".

Solution 2

Mark,

I believe you want to use android:scaleType="fitXY".

Solution 3

Please try this one works for me:

<ImageView android:id="@+id/image_view"     
  android:layout_width="wrap_content"  
  android:layout_height="wrap_content"  
  android:adjustViewBounds="true"  
  android:maxWidth="60dp" 
  android:layout_gravity="center" 
  android:maxHeight="60dp"  
  android:scaleType="fitCenter"  
  android:src="@drawable/icon"  
  /> 
Share:
191,386
Mark Manickaraj
Author by

Mark Manickaraj

Updated on August 30, 2021

Comments

  • Mark Manickaraj
    Mark Manickaraj almost 3 years

    I have an image that is too big to fit on the screen, and I want it fairly small on screen. How do I change the size of the image through XML?

    I tried:

    <ImageView
    android:id="@+id/image"
    android:layout_width = "100dp"
    android:layout_height= "100dp"
    android:scaleType="center"
    android:layout_gravity="center_horizontal|bottom"
    android:src="@drawable/dashboard_rpm_bottom"
    >
    </ImageView>
    

    But the image isn't resized... it gets cropped. Any ideas?

  • Oleg Golomoz
    Oleg Golomoz over 3 years
    So you just copypasted ameyume's answer 8 years later?
  • Oriol Roma
    Oriol Roma almost 3 years
    Be aware that fitXY distorts the aspect ratio of the image