Difference between app:srcCompat and android:src in Android's layout XML
app:srcCompat
is the most foolproof method of integrating vector drawables into your app.Vector drawables allow you to replace multiple png assets with a single vector graphic, defined in XML. While previously limited to Lollipop and higher devices
Note
As of Android Support Library 23.3.0, support vector drawables can only be loaded via app:srcCompat
.
you need to add vectorDrawables.useSupportLibrary = true to your build.gradle
file
// Gradle Plugin 2.0+
android {
defaultConfig {
vectorDrawables.useSupportLibrary = true
}
}
android:src
Sets a drawable as the content of this ImageView.It will display in
its original size. No automatic scaling .
Is there still any difference between android:src vs app:srcCompat?
Yes, there is a difference. There is an article here about it.
Basically app:srcCompat
will work on older APIs when loading up vector drawables. You’ll find directly referencing vector drawables outside of app:srcCompat
will fail prior to Lollipop, you are probably running the android:src
on Lollipop or higher.
Difference(s): android:src and tools:src?
If you are using android:src
in xml during runtime this will show up on the app, whereas if you use tools:src
in xml it will show only on the preview of Android studio and not on the runtime of the app.
Why is my ImageView not showing the image?
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>
Android notification custom layout XML imageview not showing
A quick tip that always helps in the debug process - add a background color to the view in question (also helps to set a specific Width and Height as well for testing) - it will enable you to see if the view is being rendered at all or not. If you see the background color, then the view is being rendered properly, but the image is not loading correctly. Without knowing this, it could be any combination of those issues.
With that aside, if you are dynamically changing the icon at runtime, I would look at your code there. If this is a static image then I would check that your resource file is a Vector drawable as that is only allowed when using app:srcCompat="@drawable/push_icon"
If your file is a non-vector drawable, then use android:src="@drawable/push_icon"
Here's a discussion that elaborates on this in more detail.
android.view.InflateException when use an imageview
Try to Replace.
android:src="@drawable/logo"
With.
app:srcCompat="@drawable/logo"
See reason.
And if it still doesn't work then check the size of the image. Some times it is not actually the inflating problem but a deeper one i.e. Memory issue leading to inflation exception. Use an image with minimum necessary size.
Hope it helps.
Related Topics
How to Start Genymotion Device
Firebase Firestore Variable Name Changed
Adb Not Finding My Device/Phone (Macos X)
How to Return to a Parent Activity Correctly
How to Use Opencv in Using Gradle
Deleting a Gallery Image After Camera Intent Photo Taken
Using Intent in an Android Application to Show Another Activity
How to Use Jni Bitmap Operations for Helping to Avoid Oom When Using Large Images
Difference Between App:Srccompat and Android:Src in Android's Layout Xml
Android Read Text Raw Resource File
Android: Coloring Part of a String Using Textview.Settext()
Change Screen Orientation Programmatically Using a Button
Android - Set Max Length of Logcat Messages