In Android, how do you add an image to LinearLayout without stretching it vertically (across Y axis)?

I want the image to stretch/shrink to fit horizontally but not vertically. Anyway of achieving this, preferably through xml, but dynamically will also do?



NOTE 2: See update at the bottom for correct answer.

NOTE: Sorry, I misread the question and saw that you wanted this to stretch in one direction. I'll amend my answer if I figure that one out but this will at least stop it from stretching at all.

You'll have to create a new drawable resource to achieve this:

<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android=""
    android:tileMode="clamp" />

Just replace your_image with the name of your resource, save this in your drawables folder, and set this as the background. This should draw the image in the top left of your linear layout and not repeat.

UPDATE: This will get the image to stretch horizontally, not vertically, and be aligned at the top of the linear layout:

<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android=""
    android:tileMode="disabled" />

Sorry for the initial mix-up.

You can set the scaleType attribuite on ImageView. Check out the possible values in the documentation.

A common use is to set scaleType="centerCrop", which will maintain the image's aspect ratio, but scale such that the width and height of the resulting scale will be equal to or greater than your actual ImageView width and height.

If you want to change the aspect ratio, you can use scaleType="fitXY".

Need Your Help

Applying setStyle to QLabel in PySide/PyQt

python qt user-interface pyqt pyside

In PySide, I am trying to change the overall style of a simple GUI window (a QLabel object), as outlined at the PySide documentation:

instance of UIViewcontroller in Swift

ios objective-c uiviewcontroller swift xcode6

How can i switch one UIViewcontroller to another UIViewController in Swift language? I 'm create a new empty project and and set UINavigationcontroller and not using story board.