Layout: how to make image to change its width and height proportionally?

Posted by Exterminator13 on Stack Overflow See other posts from Stack Overflow or by Exterminator13
Published on 2012-10-16T08:40:49Z Indexed on 2012/10/16 11:01 UTC
Read the original article Hit count: 181

Filed under:
|

I have such layout:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">

<TextView
    android:id="@+id/title"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_centerVertical="true"
    android:layout_toLeftOf="@+id/my_image"
    android:ellipsize="end"
    android:singleLine="true"
    android:text="Some text"
    android:textAppearance="?android:attr/textAppearanceMedium" />

<ImageView
    android:id="@+id/my_image"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignTop="@+id/title"
    android:layout_alignBottom="@+id/title"
    android:layout_alignParentRight="true"
    android:layout_centerVertical="true"
    android:adjustViewBounds="true"
    android:src="@drawable/my_bitmap_image" />

This layout does almost what I need: it makes image view height the same as text view. The image graphic contents stretched also keeping aspect ratio.

But, the width of the image view does not change! As a result, I have a wide gap between text and the image view! As a temporal solution, I override View#onLayout.

The question: how to change image width in xml layout?

UPDATE:

This is a final layout I need (text + a few images). Look at the first image: its width should be exactly the same as scaled image in it with no paddings and margins:

enter image description here

© Stack Overflow or respective owner

Related posts about android

Related posts about android-layout