hpinhal
hpinhal

Reputation: 512

Adjust ImageView width according to height

I am trying to make a simple LinearLayout composed of an ImageView and TextView.

The ImageView should scale to match the LinearLayout height and not lose proportions while doing so.

This is the xml I currently have.

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <ImageView
        android:id="@+id/logo"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:adjustViewBounds="true"
        android:background="#CCCCCC"
        android:scaleType="fitCenter"
        android:src="@drawable/strip" />

    <TextView
        android:id="@+id/logoText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ellipsize="marquee"
        android:singleLine="true"
        android:text="what an awesome text"
        android:textSize="18sp" />

</LinearLayout>

Using the above xml, the result is that the ImageView height will indeed match the LinearLayout height and it's width will be the same as the src image but the rendered image will scale properly and center, but this leaves the ImageView itself filling about 90% of the Layout's width as it's the src image width, leaving no space for the TextView.

I just would like to scale the ImageView to match the parent's height and it's width should be just as much needed to scale it proportionately.

Upvotes: 1

Views: 917

Answers (5)

sifr_dot_in
sifr_dot_in

Reputation: 3593

this visual guide is a lot helpful.
link
in your image view add:

android:scaleType="fitCenter"

or

android:scaleType="fitXY"

whichever suites you.

Upvotes: 0

KISHORE_ZE
KISHORE_ZE

Reputation: 1466

Add a linear layout on the image view. Just the image view and nothing else. :) hope it helps

EDIT 1: As many of the other answers mentioned Using the layout_weight property will also help resolve this issue.

Upvotes: 2

Oleksandr Berdnikov
Oleksandr Berdnikov

Reputation: 679

As I mentioned in the comment under your answer post where you cite KISHORE_ZE, it seems to be helpful only with an image of particular size, and on a particular screen density and resolution. So while it looks acceptable on your current device, it might be a mess on a different one. I suggest that you figure out what part of the layout you want to use for the image and use android:weight attributes for your ImageView and TextView to achieve wanted result.

Upvotes: 1

dione llorera
dione llorera

Reputation: 356

add weight 1 on imageview so there will be space for the textview

Upvotes: 1

hpinhal
hpinhal

Reputation: 512

As @KISHORE_ZE said, this solves the problem.

"Put the image view in a linear layout. Just the image view. Nothing else. "

Upvotes: 0

Related Questions