Why doesn’t my image appear when running the app?

Asked

Viewed 6,930 times

7

I have the following xml code in Android Studio:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:layout_width = "match_parent"
          android:layout_height = "match_parent"
          android:orientation = "vertical"
          android:weightSum = "1">

<ImageView
    app:srcCompat = "@mipmap/logon_img"
    android:id = "@+id/imageView"
    android:layout_gravity = "center_horizontal"
    android:contentDescription="@string/app_name"
    android:layout_width = "match_parent"
    android:layout_weight = "0.38"
    android:layout_height = "100dp"/>

<TextView
    android:id = "@+id/login"
    android:layout_gravity = "center_horizontal"
    android:textSize = "30sp"
    android:textAlignment = "center"
    android:textStyle = "normal|bold"
    android:textColor = "@color/Red"
    android:text = "@string/typeUserName"
    android:layout_weight = "0.00"
    android:layout_width = "350dp"
    android:layout_height = "wrap_content"/>

<EditText
    android:layout_height = "wrap_content"
    android:inputType = "textPersonName"
    android:ems = "10"
    android:id = "@+id/account"
    android:layout_gravity = "center_horizontal"
    android:textAlignment = "center"
    android:textSize = "20sp"
    android:layout_width = "300dp"
    tools:ignore = "LabelFor"/>

<TextView
    android:text = "@string/password"
    android:layout_width = "350dp"
    android:layout_height = "wrap_content"
    android:id = "@+id/textView2"
    android:layout_weight = "0.00"
    android:layout_gravity = "center_horizontal"
    android:textSize = "30sp"
    android:textAlignment = "center"
    android:textStyle = "normal|bold"
    android:textColor = "@color/Red"/>

<EditText
    android:layout_width = "300dp"
    android:layout_height = "wrap_content"
    android:inputType = "textPassword"
    android:ems = "10"
    android:id = "@+id/password"
    android:layout_gravity = "center_horizontal"
    android:textAlignment = "center"
    android:textSize = "20sp"
    tools:ignore = "LabelFor"/>

<Button
    android:text = "@android:string/ok"
    android:layout_height = "wrap_content"
    android:id = "@+id/button"
    android:layout_gravity = "center_horizontal"
    android:layout_width = "200dp"
    android:textSize = "20sp"
    />

</LinearLayout>

The component in question is Imageview, it simply does not appear in the emulator or on my phone, however, there is an empty space reserved for it on the screen. I searched the problem several times and in the vast majority of them was the huge size of the image, which is not my case because its size is 256x256. Follow a screenshot of the emulator and layout.

minSdkVersion 19
targetSdkVersion 25
com.android.support:appcompat-v7:25.0.0

SS do emulador SS do layout

  • I tested your layout and it was correctly displayed. Enter in the question which is the min,max and target api and the appcompat version.

  • 1

    Although I find no justification for this try to replace app:srcCompat = "@mipmap/logon_img" for android:src = "@mipmap/logon_img".

  • android:src was the solution @ramaral, mt thanks! agr I’ll look for why this happens

  • Please reply to my first comment, add the Android version of the device/emulator you tested. This will help to find a justification.

  • I changed the question, see if that’s what you need to know or if you need any more information.

  • No, you are using recent versions. I find no explanation, it should work with any of the attributes.

Show 1 more comment

2 answers

5


I tested its layout and it was correctly presented.

The only thing I see that might be creating the problem is the use of the attribute app:srcCompat.
Despite the main reason for the existence of the attribute app:srcCompat enable the use of Vectordrawable in older versions of Android, it also (should) supports(r) the use of bitmaps.

So replace

app:srcCompat = "@mipmap/logon_img"

for

android:src = "@mipmap/logon_img"

If @mipmap/logon_img is a Vectordrawable see this reply

2

I’ve had some problems with images on android, many of them disappeared with the use of a library called Picasso.

This situation occurs because the image is not "fitting" within the layout size, in the lines:

android:layout_width = "match_parent"
android:layout_weight = "0.38"
android:layout_height = "100dp"

You can try to "fit" the image by decreasing its resolution, using tools such as resize (free tool and online). Or change the size in the layout itself.

But I guide you to try to implement the Picasso. To do this simply insert into the dependencies of the build.gradle:

dependencies {
.
.
.
compile 'com.squareup.picasso:picasso:2.5.2
}

Remove the line app:srcCompat = "@mipmap/logon_img" of xml and in his Activity within the onCreate, insert the line:

Picasso.with(getApplication()).load(R.mipmap.logo_img).resize(256, 256).centerCrop().into(imageView);

If it doesn’t work, the image may be much larger than you would like or not appear, inside the .resize(x,y) you can put a ratio to fit your image in the layout.

  • 1

    It will not be exaggerated to use a library only to resize a 256x256 image. If it is an image size problem (which I don’t believe) it wouldn’t be better, since it is placed in the mipmap folder, resize it in an image editing program?

  • Yes, if it uses only one image in the application. In the course of development, I believe it has to reuse the same process.

  • The ideal would be to create all the images with all the correct sizes and advise to create these different sizes on mipmap. But once again, if it’s a single image, why not use the library? It won’t slow down the application. In my projects, I had no problems with this implementation, on the contrary, the result is much faster and satisfactory.

  • I’m not saying that library use isn’t useful in certain situations. I am simply saying that it is not the case in cases similar to this, where it is being used icons and not photographs. I’m pretty sure that the AP problem has nothing to do with the size of the image since it has 256x256.

  • This image resolution may or may not fit the AP layout.It will depend a lot on the density that the device has, for the image size to be displayed. Unfortunately, android does not make it much easier for this, the android_developer always indicates the use of mipmaps, since android does not directly convert pixels per dpi, you need to do this manually. So much so that it was my first orientation, in response, to resize this image. The problem is: it will have to create all possible dimensions and insert them into each folder

Browser other questions tagged

You are not signed in. Login or sign up in order to post.