[android] Android ImageView Fixing Image Size

I have an Android Application, where I have an ImageView, I need to keep the size constant, there can be various images that I need to put into this ImageView, of different sizes.

I just need to ensure that all the Images must fit into the ImageView, the Image should increase in size if it is smaller and should decrease, in case it is bigger.

Thanks a lot for your time.

This question is related to android imageview android-imageview

The answer is


Fix ImageView's size with dp or fill_parent and set android:scaleType to fitXY.


Try this

ImageView img
    Bitmap bmp;
    int width=100;
    int height=100;
    img=(ImageView)findViewById(R.id.imgView);
    bmp=BitmapFactory.decodeResource(getResources(),R.drawable.image);//image is your image                                                            
    bmp=Bitmap.createScaledBitmap(bmp, width,height, true);
    img.setImageBitmap(bmp);

Or If you want to load complete image size in memory then you can use

<ImageView
    android:id="@+id/img"
    android:layout_width="100dp"
    android:layout_height="100dp"
    android:src="@drawable/image" 
    android:scaleType="fitXY"/>

In your case you need to

  1. Fix the ImageView's size. You need to use dp unit so that it will look the same in all devices.
  2. Set android:scaleType to fitXY

Below is an example:

<ImageView
    android:id="@+id/photo"
    android:layout_width="200dp"
    android:layout_height="100dp"
    android:src="@drawable/iclauncher" 
    android:scaleType="fitXY"/>

For more information regarding ImageView scaleType please refer to the developer website.


I had the same issue and this helped me.

<ImageView
    android:id="@+id/image"
    android:layout_width="100dp"
    android:layout_height="100dp"
    android:scaleType="fitXY"
    />

You can also try this, suppose if you want to make a back image button and you have "500x500 png" and want it to fit in small button size.

Use dp to fix ImageView's size.

add this line of code to your Imageview.

android:scaleType="fitXY"

EXAMPLE:

<ImageView
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:id="@+id/imageView2"
        android:src="@drawable/Backicon"
        android:scaleType="fitXY"
        />

Examples related to android

Under what circumstances can I call findViewById with an Options Menu / Action Bar item? How to implement a simple scenario the OO way My eclipse won't open, i download the bundle pack it keeps saying error log getting " (1) no such column: _id10 " error java doesn't run if structure inside of onclick listener Cannot retrieve string(s) from preferences (settings) strange error in my Animation Drawable how to put image in a bundle and pass it to another activity FragmentActivity to Fragment A failure occurred while executing com.android.build.gradle.internal.tasks

Examples related to imageview

How to show imageView full screen on imageView click? How can I show an image using the ImageView component in javafx and fxml? ImageView in circular through xml ImageView rounded corners How to set tint for an image view programmatically in android? how to set image from url for imageView Scale Image to fill ImageView width and keep aspect ratio Change Image of ImageView programmatically in Android How to create a circular ImageView in Android? Full screen background image in an activity

Examples related to android-imageview

How to set a ripple effect on textview or imageview on Android? Placing a textview on top of imageview in android How to create a circular ImageView in Android? How to download and save an image in Android Android ImageView Fixing Image Size "Bitmap too large to be uploaded into a texture" Android Camera : data intent returns null 'Missing contentDescription attribute on image' in XML Android ImageView Zoom-in and Zoom-Out findViewById in Fragment