Android vector drawable app:srcCompat not showing images
Original Answer
Use android.support.v7.widget.AppCompatImageView
instead of ImageView
in your layout, like this:
<LinearLayout
...
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<android.support.v7.widget.AppCompatImageView
android:tint="#d74313"
app:srcCompat="@drawable/circle_icon"
android:layout_width="30sp"
android:layout_height="30sp" />
<android.support.v7.widget.AppCompatImageView
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
app:srcCompat="@drawable/lauzaviete"
android:layout_width="25dp"
android:layout_height="25dp" />
</LinearLayout>
See the AppCompatImageView
docs here and app:srcCompat
here.
Also, make sure to do the following:
Setup your build.gradle
android {
defaultConfig {
vectorDrawables {
useSupportLibrary = true
}
}
}
Docs: https://google.github.io/android-gradle-dsl/current/com.android.build.gradle.internal.dsl.VectorDrawablesOptions.html#com.android.build.gradle.internal.dsl.VectorDrawablesOptions:useSupportLibrary
Extend your Activity
with AppCompatActivity
public final class MainActivity extends AppCompatActivity {
@Override protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
When using app:srcCompat
, make sure to have the correct declarations in your layout:
<LinearLayout
...
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
...
</LinearLayout>
Optional (warning: please read docs): setCompatVectorFromResourcesEnabled
in your Application
class
public class App extends Application {
@Override public void onCreate() {
super.onCreate();
// Make sure we use vector drawables
AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);
}
}
Docs: https://developer.android.com/reference/android/support/v7/app/AppCompatDelegate.html#setCompatVectorFromResourcesEnabled(boolean)
VectorDrawable not showing or drawed wrong
VectorDrawable
added in API level 21.
Support Library 23.2 or higher provides full support to Vector
Drawables and Animated Vector Drawables on devices running Android 5.0
(API level 21) or lower.
You should use
app:srcCompat="@drawable/your_vector_image"
You need to add vectorDrawables.useSupportLibrary = true
to your build.gradle
file:
// Gradle Plugin 2.0+
android {
defaultConfig {
vectorDrawables.useSupportLibrary = true
}
}
FYI
app:srcCompat
attribute to reference vector drawables as well as any
other drawable available to android:src.
Vector drawable not showing in app icon below 5.0 api level
Try adding your icon as an "Image Asset," so that all the different DPI (dots per inch) versions of the icon are created.
This will make sure that your icon is handled even at lower resolution versions.
Related Topics
Drawable Folders in Res Folder
Android: What Are the Recommended Configurations for Proguard
How to Get Activity's Content View
How to Check an Android Device Is Hdpi Screen or Mdpi Screen
Android Place Picker Closes Immediately After Launch
How to Listen to Doubletap on a View in Android
Activity Not Started, Its Current Task Has Been Brought to the Front
Android Play Services 6.5: Locationclient Is Missing
File Res/Drawable/Abc_Ic_Ab_Back_Material.Xml from Drawable Resource Id #0X7F020016
How to Hide Underbar in Edittext
How to Log Request and Response Body with Retrofit-Android
Android Exception: Did You Forget to Call 'Public Void Setup (Localactivitymanager Activitygroup)'
How to Auto-Start an Android Application
What Method Should I Use Now Since Firebaseinstanceid.Getinstance().Gettoken() Is Deprecated