Image View not Wrapping Contents

JavaAndroidImageview

Java Problem Overview


I've got an ImageView wrapping this image:

<ImageView
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:scaleType="fitStart"
    android:src="@drawable/oncmap"/>

and right below it, a TextView. Unfortunately, it either pushes it down the view or out of the view depending on the device's screen size.

http://i.imgur.com/CuVFK5P.png

http://i.imgur.com/6wzMebV.jpg

I can "hack" it, but it'd rather not...

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="16dp"
android:orientation="vertical"
tools:context="MapFragment">

<ImageView
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:src="@drawable/oncmap"/>

<TextView
    android:layout_height="wrap_content"
    android:layout_width="fill_parent"
    android:text="Neptune"
    style="@style/sectionHeader"/>

<TextView
    android:layout_height="wrap_content"
    android:layout_width="fill_parent"
    android:paddingLeft="8dp"
    android:paddingRight="8dp"
    android:paddingTop="16dp"
    android:text="@string/info"
    style="@style/text"/>

Java Solutions


Solution 1 - Java

Add the following fields to ImageView:

android:scaleType="fitXY"
android:adjustViewBounds="true"

Solution 2 - Java

To display original image size in Imageview write down following. android:layout_height="wrap_content" and android:adjustViewBounds="true" do the job. No need to set ScaleType.

<ImageView
     android:id="@+id/iv_QuestionImage"
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:layout_margin="10dp"
     android:adjustViewBounds="true"
     android:src="@drawable/ic_app_icon_512"
     android:visibility="gone"
     app:layout_constraintLeft_toLeftOf="parent"
     app:layout_constraintRight_toRightOf="parent"
     app:layout_constraintTop_toTopOf="parent" />

Solution 3 - Java

if you try to add the large size image for eg let say 4~6 mb and you are using the drawable,then you would get the error like->java.lang.RuntimeException: Canvas: trying to draw too large(537528960bytes) bitmap

For java do this in your android studio, but the concept remains same for others also,do this to insert the image in your app in the background, applicable for both large and small size images.

Basically move your image in the (hi-res) drawable to drawable-xxhdpi which would be finded by going in the app->res->mipmap.

<ImageView
        android:id="@+id/imageView2"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:adjustViewBounds="true"
        android:scaleType="centerCrop"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@mipmap/imagename" />

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionTyler SebastianView Question on Stackoverflow
Solution 1 - Javasachin10View Answer on Stackoverflow
Solution 2 - JavaDharmishthaView Answer on Stackoverflow
Solution 3 - JavaLavish GargView Answer on Stackoverflow