Ait Bri
Ait Bri

Reputation: 587

ImageView adjustViewBounds does not seems to work??(Different project different outcome)

I was trying to use ImageView that would match the parent width while maintaining aspect ratio. The outcome was as expected in 1 project. But in other case, the same code gave the different result.

FYI, the layout are from two different project, not same project.Can anyone help me?

![Project 1 Layout ][1]

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#000000" >

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

</RelativeLayout>

![Project 2 layout][2]

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#000000" >

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

</RelativeLayout>

Upvotes: 1

Views: 353

Answers (2)

Ismaran Duwadi
Ismaran Duwadi

Reputation: 1539

You should set your targetSDK 19 it gave the expected result. But when set to 17 then problem came. And from documentation, this should be the reason.

Note from the docs:

If the application targets API level 17 or lower, adjustViewBounds will allow the drawable to shrink the view bounds, but not grow to fill available measured space in all cases. This is for compatibility with legacy MeasureSpec and RelativeLayout behavior.

Upvotes: 3

X.Otano
X.Otano

Reputation: 2169

Change your code to:

<ImageView
android:layout_width="match_parent"
android:layout_height="150dp"
android:adjustViewBounds="true"
android:scaleType="fitCenter"
android:src="@drawable/news_holder" />

You should set a height to let know the imageview it has a height to fill.In this case 150dp but you can set whatever you need.

Upvotes: 0

Related Questions