1

I have an android app with 2 activities: activity_main and activity_login.

On top of my activities I have a logo. It is showing up fine in activity_main but not in the activity_login.

The strange this is they both use similar layout and similar code.

Here is my activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".app.MainActivity"
    android:background="@drawable/background_1">

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:srcCompat="@drawable/logo"
        app:layout_constraintLeft_toLeftOf="parent"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:layout_marginTop="36dp" />

    <Button
        android:id="@+id/button1"
        android:layout_width="195dp"
        android:layout_height="45dp"
        android:text="Sign a File"
        android:layout_marginTop="53dp"
        app:layout_constraintTop_toBottomOf="@+id/imageView"
        android:layout_marginRight="8dp"
        app:layout_constraintRight_toRightOf="parent"
        android:layout_marginLeft="8dp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintHorizontal_bias="0.502" />

    <Button
        android:id="@+id/button2"
        android:layout_width="195dp"
        android:layout_height="45dp"
        android:text="Button"
        android:layout_marginLeft="8dp"
        app:layout_constraintLeft_toLeftOf="parent"
        android:layout_marginRight="8dp"
        app:layout_constraintRight_toRightOf="parent"
        android:layout_marginTop="30dp"
        app:layout_constraintTop_toBottomOf="@+id/button4"
        app:layout_constraintHorizontal_bias="0.502" />

    <Button
        android:id="@+id/button3"
        android:layout_width="195dp"
        android:layout_height="45dp"
        android:text="Button"
        android:layout_marginTop="34dp"
        app:layout_constraintTop_toBottomOf="@+id/button1"
        android:layout_marginRight="8dp"
        app:layout_constraintRight_toRightOf="parent"
        android:layout_marginLeft="8dp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintHorizontal_bias="0.502" />

    <Button
        android:id="@+id/button4"
        android:layout_width="195dp"
        android:layout_height="45dp"
        android:text="Button"
        android:layout_marginTop="31dp"
        app:layout_constraintTop_toBottomOf="@+id/button3"
        app:layout_constraintBottom_toBottomOf="parent"
        android:layout_marginBottom="8dp"
        app:layout_constraintVertical_bias="0.043"
        android:layout_marginRight="8dp"
        app:layout_constraintRight_toRightOf="parent"
        android:layout_marginLeft="8dp"
        app:layout_constraintLeft_toLeftOf="parent" />







</android.support.constraint.ConstraintLayout>

And, here is my activity_login.xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".login.LoginActivity"
    android:background="@drawable/background_1">

     <ImageView
    android:id="@+id/imageView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:srcCompat="@drawable/logo"
    app:layout_constraintLeft_toLeftOf="parent"
    android:layout_marginLeft="8dp"
    android:layout_marginRight="8dp"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    android:layout_marginTop="36dp" />

     <EditText
        android:id="@+id/usernameTextField"
        android:layout_width="291dp"
        android:layout_height="63dp"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginTop="53dp"
        android:background="@color/editTextBackground"
        android:ems="10"
        android:inputType="textPersonName"
        android:text="Username"
        android:textColor="#000000"
        app:layout_constraintHorizontal_bias="0.506"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/imageView" />

    <EditText
        android:id="@+id/passwordTextField"
        android:layout_width="291dp"
        android:layout_height="63dp"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginTop="0dp"
        android:background="@color/editTextBackground"
        android:ems="10"
        android:inputType="textPassword"
        android:text="Password"
        android:textColor="#000000"
        app:layout_constraintHorizontal_bias="0.506"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/usernameTextField" />

    <Button
        android:id="@+id/button"
        android:onClick="loginButtonClick"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginTop="66dp"
        android:background="@color/editTextBackground"
        android:text="Log In"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/passwordTextField"
        app:layout_constraintBottom_toBottomOf="parent"
        android:layout_marginBottom="8dp" />



</android.support.constraint.ConstraintLayout>

In the design view of the activity_login it's displaying the image fine. But, when I run a test on emulator/phone it just doesn't show.

I tried removing all the other views from login_activity and still got the same problem. I also tried making a new activity xml with the same ConstarintLayout where I just add logo as imageView, and when I run the app the logo is not showing up.

And, that same logo using the same imageView in constraintLayout works fine in my activity_main

UPDATE: Why the downvote?

UPDATE2: As adviced by rafsanahmad007 this works:

instead of:

app:srcCompat="@drawable/logo" use:

android:src="@drawable/logo"

But, it still doesn't explain why it was working using app:srcCompat in one activity and not the other one.

user3362334
  • 1,980
  • 3
  • 26
  • 58

4 Answers4

2

instead of:

app:srcCompat="@drawable/logo"

use:

android:src="@drawable/logo"

Here is a good answer to know about the difference.

it is a very common mistake. But i would suggest to use android:src as it is more safe in all appcompat versions.

If you are using vector drawable in your app ,then you can go for app:srcCompat . But you need to check the compatibility in gradle.

Community
  • 1
  • 1
rafsanahmad007
  • 23,683
  • 6
  • 47
  • 62
2

Try to use android:src="image_location" rather then app:srcCompat="image_location".

Vishal Yadav
  • 3,642
  • 3
  • 25
  • 42
Shaifali Rajput
  • 1,279
  • 12
  • 30
1

Might be you have used the same id for both the image view android:id="@+id/imageView" in different activities, So one who loads first will be appear and second one will not if the problem persist again use android:src="image_location" rather to use app:srcCompat="image_location"

Hope it will work for you.

Vishal Yadav
  • 3,642
  • 3
  • 25
  • 42
Shashwat Gupta
  • 876
  • 9
  • 22
0

Use android:src="@drawable/logo" instead of app:srcCompat="@drawable/logo".

Ferdous Ahamed
  • 21,438
  • 5
  • 52
  • 61