Why does not my image appear when I run the app?

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 just does not appear on the emulator or my cell phone, but there is an empty space reserved for it on the screen. I looked for the problem several times and the vast majority of them was the huge size of the image, which is not my case because its size is 256x256. Here's a screenshot of the emulator and layout.

  

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

    
asked by anonymous 27.10.2016 / 02:38

2 answers

5

I tested your layout and it was displayed correctly.

The only thing you see that might be creating the problem is the use of the app:srcCompat attribute. Although the main reason for the existence of the app:srcCompat attribute is to enable VectorDrawable in older versions of Android, it also (should) support the use of bitmaps.

So, replace

app:srcCompat = "@mipmap/logon_img"

by

android:src = "@mipmap/logon_img"

If @mipmap/logon_img is a VectorDrawable see this response

    
27.10.2016 / 15:05
2

I've had some problems with images on android, many of them disappeared using a library called picasso .

This situation occurs because the image is not "fitting" within the size of the layout 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 lowering its resolution, using tools like resize ). Or change the size in the layout itself.

But I advise you to try to implement picasso . To do this, simply enter in the dependencies of build.gradle :

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

Remove the line app:srcCompat = "@mipmap/logon_img" from xml and its Activity within onCreate , enter the line:

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

If it does not work, the image can be much larger than you would like or even appear, within .resize(x,y) you can put a ratio to fit your image in the layout.

    
27.10.2016 / 12:34