Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6588981
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T17:06:55+00:00 2026-05-25T17:06:55+00:00

I have an image which is smaller than the container I would like it

  • 0

I have an image which is smaller than the container I would like it to fit inside of. I would like the image to stretch, keeping it’s aspect ratio, to it’s largest possible size.

To illustrate this problem:

<ImageView  android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:src="@drawable/thumbnail"
            android:scaleType="fitXY"
            android:adjustViewBounds="true"/>

The ImageView above would be stretched to fill the width of the container. The @drawable it contained would also stretch along the x axis to fit the width of ImageView which is perfect. The problem however is that the dimension labelled wrap_content, in this case height, remains the same size as the @drawables initial height.

I have read the documentation regarding ScaleType here and can’t find the answer there.

The following image describes the above code:

enter image description here enter image description here

  Current behaviour               Desired Behaviour

Edit

An ImageView given scaleType="fitCenter" will accurately expand/shrink the @drawable inside of it to grow as large as possible while retaining it’s aspect ratio.

The ImageViews dimensions are defined before the @drawable is scaled in any way. The ImageView dimensions are not effected by scaling of it’s contained @drawable.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T17:06:56+00:00Added an answer on May 25, 2026 at 5:06 pm

    XML

    The only solution to this in XML is to use "match_parent" or a discrete maximum value instead of "wrap_content" where possible. This will ensure the ImageView is the correct size, which will then meaning adding scaleType="fitCenter" will ensure the @drawable will then scale correctly.

    Programatically

    It’s ugly, but you can resize the ImageView after it’s dimensions have been given discrete values:

        final ImageView thumbnailView = (ImageView) toReturn.findViewById(R.id.thumbnail);  
    
        ViewTreeObserver thumbnailViewVto = thumbnailView.getViewTreeObserver();
        thumbnailViewVto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
            private boolean changed = false;
            @Override
            public void onGlobalLayout() {
                if(!changed) {
                    Drawable image = getResources().getDrawable(R.drawable.thumbnail);
                    float heighToWidthRatio = image.getIntrinsicWidth()/image.getIntrinsicHeight();
                    int height = thumbnailView.getHeight();
    
                    thumbnailView.setLayoutParams(
                            new LayoutParams(
                                    (int) (height * heighToWidthRatio), height));
                    changed = true;
                }
            }
        });
    

    EDIT

        final ImageView thumbnailView = (ImageView) toReturn.findViewById(R.id.thumbnail);  
    
        thumbnailView.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
    
            @Override
            public void onGlobalLayout() {
                // Remove the GlobalOnLayout Listener so it only fires once.
                thumbnailView.getViewTreeObserver().removeGlobalOnLayoutListener(this)
    
                // Find the images Height to Width ratio
                Drawable image = getResources().getDrawable(R.drawable.thumbnail);
                float heighToWidthRatio = image.getIntrinsicWidth()/image.getIntrinsicHeight();
    
                // Use this ratio to discover the ratio of the ImageView to allow it to perfectly contain the image.
                int height = thumbnailView.getHeight();
                thumbnailView.setLayoutParams(new LayoutParams(
                                    (int) (height * heighToWidthRatio), height));
            }
        });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an image as a header which when the screen is smaller, for
I have taken thumbnail image from gallery which is smaller in size and resized
I have a view and template called index.html. I have a image which is
Hope someone has an easy answer on this. I have a header image which
I have an object of type IMAGE which holds image. I wanted to display
I have a small (100x80) overlay image which will be placed at x=40, y=50
How to do the following: I want to have a link [upload image] which
I have an image on my webpage in which I want to have text
I have an image uploader and cropper which creates thumbnails and I occasionally get
I have a binary image (a dll) which I wish to view the assembly

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.