76

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.

https://i.stack.imgur.com/t0yo0.png

https://i.stack.imgur.com/biNIW.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"/>

Shyam
  • 6,376
  • 1
  • 24
  • 38
Tyler Sebastian
  • 9,067
  • 6
  • 39
  • 62

3 Answers3

288

Add the following fields to ImageView:

android:scaleType="fitXY"
android:adjustViewBounds="true"
Shyam
  • 6,376
  • 1
  • 24
  • 38
  • 28
    scaleType is not really needed. adjustViewBounds does the job. But can someone explain why this issue is happening? – Ajith M A Sep 19 '15 at 17:55
  • 1
    @AjithMemana See the documentation: http://developer.android.com/reference/android/widget/ImageView.html – easycheese Jan 09 '16 at 22:44
3

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" />
Dharmishtha
  • 1,313
  • 10
  • 21
-1

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" />
Lavish Garg
  • 235
  • 2
  • 9