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?
IT MUST BE FOR LINEARLAYOUT IS THE KEY.
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="http://schemas.android.com/apk/res/android" android:src="@drawable/your_image" 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="http://schemas.android.com/apk/res/android" android:src="@drawable/your_image" android:gravity="fill_horizontal|clip_vertical|top" 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".