51

I have an ImageView that I want to display matching the full width of the device, I realized that like this:

<ImageView
    android:id="@+id/home_bar_newvault"
    android:layout_width="wrap_content"
    android:layout_height="40dp"
    android:scaleType="centerCrop"
    android:layout_alignParentBottom="true"
    android:src="@drawable/home_bar" />

The home_bar is a PNG image file with the following dimensions: 2399x254. When I choose to view the Graphical Layout of the UI it displays the imageview correctly at the bottom of the activity. However, when I start the application on my device it won't display the Imageview at all.

Tim Kranen
  • 4,202
  • 4
  • 26
  • 49
  • Did you set the target screen and resolution of the `Graphical Layout` as same as your real device's? – user2652394 Aug 15 '13 at 10:54
  • Yes, I've tried every resolution and target screen and they all display the ImageView in the Graphical Layout. I only have one physical device for the actual testing though, maybe a device related problem? – Tim Kranen Aug 15 '13 at 10:57
  • [See this answer](https://stackoverflow.com/a/29067760/4084269). If the image is too large, the logcat will show a message like this one: `Bitmap too large to be uploaded into a texture (2496x4437, max=4096x4096)` – Josselin Oct 05 '17 at 18:01

12 Answers12

104

I had the same issue, it is only showing in Design tab for Android Studio 2.2.

What I did to make it work is to change the automatic key assigned (populated via Drag/Drop ImageView) from app:srcCompat="@drawable/logo" to android:src="@drawable/logo" in Text tab to make it appear on both the emulator and the device e.g

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/logo"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="75dp"
    android:id="@+id/logoImageView"
    android:scaleType="fitXY"
    android:scaleX="1.5"
    android:scaleY="1.5" />
Woppi
  • 5,303
  • 11
  • 57
  • 81
  • 1
    https://stackoverflow.com/questions/40624554/android-what-is-the-difference-between-appsrccompat-and-androidsrc Instead, you can easily add support for app:srcCompat – Jayz7522 Sep 25 '17 at 15:27
  • 2
    Is there a way to get AS to stop doing that by default? – eric Nov 22 '17 at 00:02
  • 3
    The same thing still happened to me in Android Studio 3.4 – Sunchezz Jun 26 '19 at 10:50
30

For me, the problem was that I was using

tools:src=...

rather than

android:src=...

It got auto-generated trying to use an already existing template image.

Maik Thielen
  • 301
  • 3
  • 2
24

Alright I've tried changing the resolution of the image (cutting it in half making it 1000x130), and somehow that fixed the problem. I think Android can't render large images like that directly? I don't know, if you know more about the subject please don't hesitate to reply! Anyway, scaling down the image worked.

Tim Kranen
  • 4,202
  • 4
  • 26
  • 49
  • 1
    Re-sizing the image worked for me as well. I had a 250x3000px image and I cut the image in half to 125x1500px and it appeared on the device. – Noah Nov 09 '14 at 06:38
  • Fixed for me too. I've seen this before too, I keep forgetting it's an issue. There really needs to be an error message for this – Wayneio Jul 23 '18 at 10:19
17

you can use:

android:background="@drawable/home_bar"

instead of:

android:src="@drawable/home_bar"
Gouda Elalfy
  • 6,888
  • 1
  • 26
  • 38
12

I had the same issue and was able to solve it using following.

My problem was that the direction of the drawable archive was wrote in the second scrCompact box.

I copy-pasted in the up box and the issue solved.

enter image description here

Imran
  • 5,542
  • 3
  • 23
  • 46
6

Use drawable-nodpi folder in res if image is too big.

Francis
  • 6,788
  • 5
  • 47
  • 64
2

None of the above did the trick, it was a really big image, found this solution: In your manifest file

<application
        //this solve it for me.
        android:hardwareAccelerated="false"
        // Add this if the previuos line didn't solve tge problem
        android:largeHeap="true"
        android:theme="@style/AppTheme">

Solve it for me.

Hope it helps someone.

Idan Magled
  • 2,186
  • 1
  • 23
  • 33
1

I had a problem with a samsung phone not displaying my drawable but it worked fine on another phone. The picture was not in any large dimensions or otherwise abnormal.

To solve this I simply put my picture asset into a drawable subfolder instead of having it in the main drawable folder. E.g from res/drawable to res/drawable-mdpi worked for me. If you have a picture to use for everything not having a specific dpi, put it into res/drawable-nodpi. Here's a bit more about the dpi folders.

ZooMagic
  • 616
  • 7
  • 15
  • "If you have a picture to use for everything not having a specific dpi, put it into res/drawable-nodpi" - It fine works for me! Thank you! – Alexander Kiselev Mar 02 '20 at 13:41
0

For my case, I change the scale type of the image

android:scaleType="fitXY"
0

(The following answer is based on the latest development (2021) of the Android Studio)

You shall use androidx.appcompat.widget.AppCompatImageView and app:srcCompat to maximize the compatibility.

The implementation shall have be included when you create your new default project as this is somehow similar to the ConstraintLayout case.

<androidx.appcompat.widget.AppCompatImageView
    android:id="@+id/home_bar_newvault"
    android:layout_width="wrap_content"
    android:layout_height="40dp"
    android:scaleType="centerCrop"
    android:layout_alignParentBottom="true"
    app:srcCompat="@drawable/home_bar" />
Chester Fung
  • 182
  • 1
  • 10
0

I have the same problem, the image was fine in the layout editor but become blured when I start the app. My solution is very simple, when copy paste the image to the project choose drawable-xxhdpi for the destination directory to make the image size smaller.

Rahmad
  • 43
  • 6
0

Convert your image to VECTOR

How do I convert pngs directly to android vector drawables?

Sometimes based on your project config... standard images were not shown in imageview and no one of above answers was worked in these situations.

But if you convert your image to android-vector(xml) then it will be shown simply in all of configuration or property-set-types (tools, background, src...)

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/ic_launcher_background"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="75dp"
    android:id="@+id/logoImageView"
    android:scaleType="fitXY"
    android:scaleX="1.5"
    android:scaleY="1.5"
    tools:ignore="MissingConstraints" />

in this sample the @drawable/ic_launcher_background is a vector :

enter image description here

Eyni Kave
  • 1,113
  • 13
  • 23