0

Trying to display an image using ImageView but it's not being displayed. The source image is kept in the drawable directory.

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/constrainLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:scaleType="centerInside"
        android:visibility="visible"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.485"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:ignore="ContentDescription"
        tools:srcCompat="@drawable/nature"/>
</androidx.constraintlayout.widget.ConstraintLayout>
homerman
  • 3,369
  • 1
  • 16
  • 32
Ubaid Khan
  • 7
  • 1
  • 8

3 Answers3

2

instead of using

        tools:srcCompat="@drawable/nature"

use

        android:src="@drawable/nature"
Wini
  • 1,906
  • 1
  • 12
  • 31
2

The tools:srcCompat="@drawable/nature" is using for showing the image in design time. When you need to see it on run time, you should add android:src="@drawable/nature" as below in your ImageView:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/constrainLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:scaleType="centerInside"
        android:visibility="visible"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.485"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:src="@drawable/nature"
        tools:ignore="ContentDescription"
        tools:srcCompat="@drawable/nature"/>
</androidx.constraintlayout.widget.ConstraintLayout>
Shalu T D
  • 3,921
  • 2
  • 26
  • 37
1

Just replace

tools:srcCompat="@drawable/nature"

by

android:src="@drawable/nature"

Ronak Sethi
  • 607
  • 5
  • 12