Hello. A few days I can not figure out why using vector xml icons on Android 4 crashes.

Here is the markup itself

<?xml version="1.0" encoding="utf-8"?> <RelativeLayout 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/main_activity_layout" android:layout_width="match_parent" android:layout_height="match_parent" app:background="@drawable/bg" tools:context="app.second.MainActivity"> <LinearLayout android:id="@+id/btn_layout" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_centerVertical="true" android:layout_gravity="center" android:orientation="vertical"> <ImageButton android:id="@+id/btn1" android:layout_width="100dp" android:layout_height="100dp" android:background="@drawable/square_blue_btn" android:padding="20dp" app:srcCompat="@drawable/icon_btn" /> <ImageButton android:id="@+id/btn2" android:layout_width="100dp" android:layout_height="100dp" android:background="@drawable/square_blue_btn" android:padding="20dp" app:srcCompat="@drawable/icon_btn" /> <ImageButton android:id="@+id/btn3" android:layout_width="100dp" android:layout_height="100dp" android:background="@drawable/square_blue_btn" android:padding="20dp" app:srcCompat="@drawable/icon_btn" /> </LinearLayout> </RelativeLayout> 

As a background, these images have vector icons in xml drawable form. In particular, the square_btn icon has this markup.

 <?xml version="1.0" encoding="utf-8"?> <vector xmlns:android="http://schemas.android.com/apk/res/android" android:width="150dp" android:height="150dp" android:viewportWidth="150" android:viewportHeight="150"> <path android:fillColor="#0761CB" android:pathData="M138.9,75.8 C139.7,100.7,139.5,130.7,109.2,136.4 C85.5,140.8,44.3,143.1,24.2,127.5 C4.5,112.2,10.8,71.4,14.6,50.2 C19.9,21,41.9,12.9,68.1,11.4 C85.3,10.4,108.9,9.3,123.1,21.5 C137.5,34,137.8,58.3,138.9,75.8 C139.3,82.8,147.7,83.4,147.2,75.9 C145.9,52.5,147.2,14.2,118.7,6 C93.1,-1.2,55.1,-0.5,29.9,8.6 C3,18.4,4,57.1,3.1,80.1 C2.1,104.9,0.7,136.9,31.4,143 C59.6,148.7,97.7,150.8,125.8,143.3 C153.3,135.9,148.1,97.3,147.2,76 C146.9,68.5,138.7,68.8,138.9,75.8 Z" /> <path android:fillColor="#3BA4FF" android:pathData="M143.1,75.9 C143.9,92.7,146.9,118.5,133.4,131.9 C119.7,145.4,93,142.8,75.8,142.5 C58.3,142.2,31.7,143.6,17.8,130.1 C4.3,116.9,6.7,92.5,7.5,75.8 C8.4,58.3,8.6,34.2,22.1,20.6 C35.4,7.2,58.6,7.2,75.7,6.9 C92.5,6.6,115.9,5.5,128.9,18.9 C142.5,32.8,142.2,58.2,143.1,75.9 Z" /> <path android:fillColor="#3BD5FF" android:pathData="M75.7,6.9 C58.6,7.2,35.4,7.2,22.1,20.6 C8.6,34.2,8.4,58.3,7.5,75.8 C7.1,84,7,92.3,7.5,100.6 C9.1,82.7,11,64.3,17,47.3 C23.1,29.8,37.1,20,54.8,16.7 C74.5,13.1,95.6,11.5,115.6,13.7 C118.7,14,121.7,14.6,124.5,15.2 C110.5,6,91.5,6.6,75.7,6.9 Z" /> <path android:fillColor="#088BF4" android:pathData="M75.7,142.5 C92.9,142.8,119.6,145.4,133.3,131.9 C146.8,118.5,143.7,92.8,143,75.9 C142.8,70.8,142.7,65.6,142.3,60.8 C141.6,64,140.5,67.4,139.7,70.4 C133.4,93,122.3,111,97.7,115.2 C75.7,119,49.2,120.5,27.8,112.4 C22.8,110.5,18.3,107.8,15.4,103.2 C12.4,98.6,11.1,92.1,9.9,85.3 C9.1,80.8,8.4,76,7.8,71.1 C7.7,72.7,7.6,74.2,7.5,75.8 C6.8,92.6,4.3,116.9,17.8,130 C31.7,143.6,58.2,142.1,75.7,142.5 Z" /> <path android:fillColor="#2198FF" android:pathData="M75.7,142.5 C96.7,142.9,133.1,146.5,140.3,119.6 C136.1,124.9,130.6,128.5,123.6,130.7 C101,137.9,74.2,136.5,50.9,136.3 C41.2,136.2,31.9,135.5,22.2,133.5 C37.8,142.7,58.6,142.1,75.7,142.5 Z" /> <path android:fillColor="#3BA4FF" android:pathData="M126.5,119.5 C127,121.4,125.8,122.5,123.9,122 C122,121.6,120.1,119.8,119.7,118 C119.3,116.2,120.5,115,122.3,115.4 C124.2,115.8,126,117.7,126.5,119.5 Z" /> <path android:fillColor="#FFFFFF" android:pathData="M49.9,25.5 C52.2,28.4,48.3,33.6,41.9,37.1 C35.4,40.6,28.8,41,26.5,37.7 S27.2,29.1,33.6,25.9 S47.7,22.6,49.9,25.5 Z" /> <path android:fillColor="#FFFFFF" android:pathData="M62,22.5 C62.1,24.5,60.3,26.1,57.9,25.9 C55.5,25.7,53.4,23.9,53.2,21.8 C53,19.8,54.8,18.3,57.2,18.5 C59.7,18.7,61.9,20.5,62,22.5 Z" /> <path android:fillColor="#0ACCFF" android:pathData="M46.8,42.6 C35.2,42.6,25.8,50.3,25.8,59.9 C25.8,69.4,35.2,77.2,46.8,77.2 S67.8,69.5,67.8,59.9 C67.8,50.3,58.4,42.6,46.8,42.6 Z M44.5,68.3 C37.9,68.3,32.5,64.3,32.5,59.5 C32.5,54.6,37.9,50.7,44.5,50.7 C51.1,50.7,56.5,54.7,56.5,59.5 C56.5,64.3,51.1,68.3,44.5,68.3 Z" /> <path android:fillColor="#0ACCFF" android:pathData="M102.1,42.6 C95.8,42.6,90.8,48.3,90.8,55.2 S95.9,67.8,102.1,67.8 C108.4,67.8,113.4,62.1,113.4,55.2 S108.4,42.6,102.1,42.6 Z M100.9,61.4 C97.3,61.4,94.4,58.5,94.4,54.9 C94.4,51.3,97.3,48.4,100.9,48.4 C104.5,48.4,107.4,51.3,107.4,54.9 C107.3,58.5,104.4,61.4,100.9,61.4 Z" /> <path android:fillColor="#0ACCFF" android:pathData="M88.6,91.4 C82,85,73.1,83.6,68.7,88.2 C64.3,92.8,66.1,101.6,72.6,107.9 C79.2,114.2,88.1,115.6,92.5,111.1 C97,106.5,95.2,97.7,88.6,91.4 Z M75.5,102.4 C71.8,98.8,70.5,94,72.8,91.6 C75.1,89.3,79.9,90.3,83.7,93.9 C87.4,97.5,88.7,102.3,86.4,104.7 C84.1,107,79.2,106,75.5,102.4 Z" /> <path android:fillColor="#0ACCFF" android:pathData="M49.1,87.5 C44.5,83,37.6,82.6,33.7,86.6 C29.9,90.6,30.5,97.5,35.2,101.9 C39.8,106.4,46.7,106.8,50.6,102.8 C54.4,98.8,53.7,92,49.1,87.5 Z M37.8,97.4 C35.2,94.9,34.6,91.1,36.6,89.1 C38.6,87.1,42.3,87.5,45,90 C47.6,92.5,48.2,96.3,46.2,98.3 C44.2,100.3,40.5,99.9,37.8,97.4 Z" /> <path android:fillColor="#0ACCFF" android:pathData="M93.3,17.3 C87.4,19.8,84.3,26,86.5,31.1 C88.7,36.2,95.3,38.3,101.2,35.7 C107.1,33.2,110.2,27,108,21.9 C105.8,16.9,99.2,14.8,93.3,17.3 Z M98,31.6 C94.6,33,91,32.1,89.9,29.5 S90.6,23.6,94,22.1 C97.4,20.7,101,21.6,102.1,24.2 S101.4,30.1,98,31.6 Z" /> <path android:fillColor="#0ACCFF" android:pathData="M113.4,71.9 C106.5,74.9,103.1,82.4,105.8,88.7 C108.5,95,116.3,97.8,123.2,94.8 C130.1,91.8,133.5,84.3,130.8,78 C128,71.6,120.3,68.9,113.4,71.9 Z M119.3,89.5 C115.4,91.2,111.1,89.9,109.7,86.7 C108.3,83.5,110.4,79.5,114.3,77.8 C118.2,76.1,122.5,77.4,123.9,80.6 C125.3,83.8,123.2,87.8,119.3,89.5 Z" /> <path android:fillColor="#0ACCFF" android:pathData="M83,61.1 C86.2585,61.1,88.9,63.8311,88.9,67.2 C88.9,70.5689,86.2585,73.3,83,73.3 C79.7415,73.3,77.1,70.5689,77.1,67.2 C77.1,63.8311,79.7415,61.1,83,61.1 Z" /> <path android:fillColor="#0ACCFF" android:pathData="M18.4,70.4 C21.3271,70.4,23.7,73.1311,23.7,76.5 C23.7,79.8689,21.3271,82.6,18.4,82.6 C15.4729,82.6,13.1,79.8689,13.1,76.5 C13.1,73.1311,15.4729,70.4,18.4,70.4 Z" /> <path android:fillColor="#0ACCFF" android:pathData="M129.7,33.7 C132.958,33.7,135.6,36.1624,135.6,39.2 C135.6,42.2376,132.958,44.7,129.7,44.7 C126.442,44.7,123.8,42.2376,123.8,39.2 C123.8,36.1624,126.442,33.7,129.7,33.7 Z" /> <path android:fillColor="#2198FF" android:pathData="M38.5,116.4 C40.8748,116.4,42.8,118.325,42.8,120.7 C42.8,123.075,40.8748,125,38.5,125 C36.1252,125,34.2,123.075,34.2,120.7 C34.2,118.325,36.1252,116.4,38.5,116.4 Z" /> </vector> 

And here I am thrown such an exception

 FATAL EXCEPTION: main java.lang.RuntimeException: Unable to start activity ComponentInfo{app.second/app.second.MainActivity}: android.view.InflateException: Binary XML file line #56: Error inflating class ImageButton at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230) at android.app.ActivityThread.access$600(ActivityThread.java:141) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:137) at android.app.ActivityThread.main(ActivityThread.java:5041) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:511) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) at dalvik.system.NativeStart.main(Native Method) Caused by: android.view.InflateException: Binary XML file line #56: Error inflating class ImageButton at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704) at android.view.LayoutInflater.rInflate(LayoutInflater.java:746) at android.view.LayoutInflater.rInflate(LayoutInflater.java:749) at android.view.LayoutInflater.rInflate(LayoutInflater.java:749) at android.view.LayoutInflater.inflate(LayoutInflater.java:489) at android.view.LayoutInflater.inflate(LayoutInflater.java:396) at android.view.LayoutInflater.inflate(LayoutInflater.java:352) at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:292) at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140) at app.second.MainActivity.onCreate(MainActivity.java:37) at android.app.Activity.performCreate(Activity.java:5104) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230) at android.app.ActivityThread.access$600(ActivityThread.java:141) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:137) at android.app.ActivityThread.main(ActivityThread.java:5041) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:511) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) at dalvik.system.NativeStart.main(Native Method) Caused by: android.content.res.Resources$NotFoundException: File res/drawable/square_blue_btn.xml from drawable resource ID #0x7f020099 at android.content.res.Resources.loadDrawable(Resources.java:1958) at android.content.res.TypedArray.getDrawable(TypedArray.java:601) at android.view.View.<init>(View.java:3330) at android.widget.ImageView.<init>(ImageView.java:114) at android.widget.ImageButton.<init>(ImageButton.java:87) at android.support.v7.widget.AppCompatImageButton.<init>(AppCompatImageButton.java:60) at android.support.v7.widget.AppCompatImageButton.<init>(AppCompatImageButton.java:56) at android.support.v7.app.AppCompatViewInflater.createView(AppCompatViewInflater.java:118) at android.support.v7.app.AppCompatDelegateImplV9.createView(AppCompatDelegateImplV9.java:1029) at android.support.v7.app.AppCompatDelegateImplV9.onCreateView(AppCompatDelegateImplV9.java:1087) at android.support.v4.view.LayoutInflaterCompatHC$FactoryWrapperHC.onCreateView(LayoutInflaterCompatHC.java:47) at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:675) at android.view.LayoutInflater.rInflate(LayoutInflater.java:746) at android.view.LayoutInflater.rInflate(LayoutInflater.java:749) at android.view.LayoutInflater.rInflate(LayoutInflater.java:749) at android.view.LayoutInflater.inflate(LayoutInflater.java:489) at android.view.LayoutInflater.inflate(LayoutInflater.java:396) at android.view.LayoutInflater.inflate(LayoutInflater.java:352) at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:292) at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140) at app.second.MainActivity.onCreate(MainActivity.java:37) at android.app.Activity.performCreate(Activity.java:5104) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230) at android.app.ActivityThread.access$600(ActivityThread.java:141) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:137) at android.app.ActivityThread.main(ActivityThread.java:5041) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:511) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) at dalvik.system.NativeStart.main(Native Method) Caused by: org.xmlpull.v1.XmlPullParserException: Binary XML file line #2: invalid drawable tag vector at android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:881) at android.graphics.drawable.Drawable.createFromXml(Drawable.java:822) at android.content.res.Resources.loadDrawable(Resources.java:1955) at android.content.res.TypedArray.getDrawable(TypedArray.java:601) at android.view.View.<init>(View.java:3330) at android.widget.ImageView.<init>(ImageView.java:114) at android.widget.ImageButton.<init>(ImageButton.java:87) at android.support.v7.widget.AppCompatImageButton.<init>(AppCompatImageButton.java:60) at android.support.v7.widget.AppCompatImageButton.<init>(AppCompatImageButton.java:56) at android.support.v7.app.AppCompatViewInflater.createView(AppCompatViewInflater.java:118) at android.support.v7.app.AppCompatDelegateImplV9.createView(AppCompatDelegateImplV9.java:1029) at android.support.v7.app.AppCompatDelegateImplV9.onCreateView(AppCompatDelegateImplV9.java:1087) at android.support.v4.view.LayoutInflaterCompatHC$FactoryWrapperHC.onCreateView(LayoutInflaterCompatHC.java:47) at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:675) at android.view.LayoutInflater.rInflate(LayoutInflater.java:746) at android.view.LayoutInflater.rInflate(LayoutInflater.java:749) at android.view.LayoutInflater.rInflate(LayoutInflater.java:749) at android.view.LayoutInflater.inflate(LayoutInflater.java:489) at android.view.LayoutInflater.inflate(LayoutInflater.java:396) at android.view.LayoutInflater.inflate(LayoutInflater.java:352) at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:292) at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140) at app.second.MainActivity.onCreate(MainActivity.java:37) at android.app.Activity.performCreate(Activity.java:5104) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230) at android.app.ActivityThread.access$600(ActivityThread.java:141) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:137) at android.app.ActivityThread.main(ActivityThread.java:5041) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:511) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) at dalvik.system.NativeStart.main(Native Method) 

The Gradle contains the following information.

 apply plugin: 'com.android.application' android { compileSdkVersion 25 buildToolsVersion "25.0.1" defaultConfig { applicationId "app.second" minSdkVersion 15 targetSdkVersion 25 versionCode 1 versionName "1.0" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" vectorDrawables.useSupportLibrary = true } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } } dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { exclude group: 'com.android.support', module: 'support-annotations' }) compile 'com.android.support:appcompat-v7:25.3.1' compile 'com.android.support.constraint:constraint-layout:1.0.2' compile 'com.android.support:support-v4:25.3.1' compile 'com.google.android.gms:play-services-ads:10.0.1' testCompile 'junit:junit:4.12' } 

Tell me please. How can you specify vector images in android lower than 5 as a background image?

  • one
    This should help: stackoverflow.com/questions/35915974/… - Vladimir Parfenov
  • Not really understand where to register it? Resources resources = context.getResources (Resources, int, Theme); Theme theme = context.getTheme (); Drawable drawable = VectorDrawableCompat.create (resources, R.drawable.drawable, theme); - Kolhoznik
  • one
    Programmatically for each view. Get Drawable this way and set your ImageView as a background. - Vladimir Parfenov
  • one
    As far as I know, no. What's so complicated? They created a method in which you pass the desired view and resource id, and there are 4 lines ... - Vladimir Parfenov
  • one
    Wrote in response. There was an error with getResources() . But it was better to think independently and google. They will not always write code for you. - Vladimir Parfenov

1 answer 1

An example of setting a vector resource in background :

 setBackground(imageView, R.drawable.your_drawable); private void setBackground(View target, @DrawableRes int resId) { Resources resources = getResources(); Resources.Theme theme = getTheme(); Drawable drawable = VectorDrawableCompat.create(resources, resId, theme); target.setBackground(drawable); } 

The getTheme() and getResources() methods are called from the context. The setBackground() method is setBackground() where you fill in the view.

  • Swears on the method target.setBackground (drawable); Writes Call requires API level 16 (current min is 15): android.view.View # setBackground - Kolhoznik
  • Is there any way for api 15? - Kolhoznik
  • Then setBackgroundDrawable(drawable) . It is deprecated, but with your version otherwise nothing. - Vladimir Parfenov
  • one
    Well, I will prescribe a condition. for 15 setBackgroundDrawable, for above versions setBackground - Kolhoznik
  • All the same, the same eksepsheny throws out - Kolhoznik