ImageView not showing vector image (SVG file)

Hello everyone !

I'm trying to use SVG file instead of multiple PNG files but unfortunately, my ImageView is not showing anything. I already tried this : https://stackoverflow.com/questions/38221986/android-vector-drawable-appsrccompat-not-showing-images

Here is my code.

task_detail.axml

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/BackgroundTest"
    android:padding="8dp"
    android:layout_weight="16">
    <android.support.v7.widget.AppCompatImageView
      android:id="@+id/imgVector"
      android:layout_centerVertical="true"
      android:layout_centerHorizontal="true"
      app:srcCompat="@drawable/link"
      android:layout_width="100dp"
      android:layout_height="100dp" />
</RelativeLayout>

task_detail.cs

class task_detail : AppCompatActivity
    {
        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);

            SetContentView(Resource.Layout.task_detail);

            //Fix for white Status Bar
            Window window = this.Window;
            window.ClearFlags(WindowManagerFlags.TranslucentStatus);
            window.AddFlags(WindowManagerFlags.DrawsSystemBarBackgrounds);
            window.SetStatusBarColor(new Color(ContextCompat.GetColor(this, Resource.Color.BackgroundTest)));

            //Trying to set SVG as src
            AppCompatDelegate.CompatVectorFromResourcesEnabled = true;
            Android.Support.V7.Widget.AppCompatImageView imgVector = FindViewById<Android.Support.V7.Widget.AppCompatImageView>(Resource.Id.imgVector);
            imgVector.SetImageResource(Resource.Drawable.link);
        }

    }

Changing xmlns:app to http://schemas.android.com/tools is giving me errors : app:elevation isn't working with it so I'm not using it.

Can you help me please ?
Thanks a lot !

PS : All my packages are up-to-date.

Posts

  • QuakeulfQuakeulf NOMember ✭✭✭

    Can I see what your SVGs look like?

  • ClementPOIRETClementPOIRET USMember

    Here is my SVG from Adobe Illustrator : https://pastebin.com/dpDXNQsx
    I got it working by using a Vector and the Path Data but I can't use the linear gradient to make it look like the original SVG. The vector is only showing a solid pink color.

    This is my new code :
    task_detail.axml

            <RelativeLayout
               android:layout_width="match_parent"
               android:layout_height="match_parent"
               android:background="@color/BackgroundTest"
               android:padding="8dp"
               android:layout_weight="16">
                <ImageView
                   android:id="@+id/imgVector"
                   android:layout_centerInParent="true"
                   android:layout_width="90dp"
                   android:layout_height="90dp"
                   app:srcCompat="@drawable/vector"/>
            </RelativeLayout>
    

    vector.xml

        <?xml version="1.0" encoding="utf-8" ?>
        <vector xmlns:android="http://schemas.android.com/apk/res/android"
               android:width="90dp"
               android:height="90dp"
               android:viewportWidth="90"
               android:viewportHeight="90"
               android:tint="@drawable/Gradient1">
            <path
               android:pathData="M46.9,18.2l-4.7,4.7c-2,2-4.7,3.1-7.5,3.1h-2.3c-0.6,0-1-0.5-1-1s0.5-1,1-1h2.3c2.3,0,4.4-0.9,6.1-2.5l4.7-4.7
            c1.6-1.6,2.5-3.8,2.5-6.1S47,6.2,45.4,4.6s-3.8-2.5-6.1-2.5c-2.3,0-4.4,0.9-6.1,2.5l-4.7,4.7c-1.6,1.6-2.5,3.8-2.5,6.1v2.3
            c0,0.6-0.5,1-1,1s-1-0.5-1-1v-2.3c0-2.8,1.1-5.5,3.1-7.5l4.7-4.7c2-2,4.7-3.1,7.5-3.1s5.5,1.1,7.5,3.1S50,7.8,50,10.7
            S48.9,16.2,46.9,18.2z M16.7,34.4c-0.3,0-0.5-0.1-0.7-0.3c-0.4-0.4-0.4-1.1,0-1.5l17.7-17.7c0.4-0.4,1.1-0.4,1.5,0
            c0.4,0.4,0.4,1.1,0,1.5L17.4,34.1C17.2,34.3,16.9,34.4,16.7,34.4z M17.7,26h-2.3c-2.3,0-4.4,0.9-6.1,2.5l-4.7,4.7
            c-1.6,1.6-2.5,3.8-2.5,6.1c0,2.3,0.9,4.4,2.5,6.1s3.8,2.5,6.1,2.5s4.4-0.9,6.1-2.5l4.7-4.7c1.6-1.6,2.5-3.8,2.5-6.1v-2.3
            c0-0.6,0.5-1,1-1s1,0.5,1,1v2.3c0,2.8-1.1,5.5-3.1,7.5l-4.7,4.7c-2,2-4.7,3.1-7.5,3.1s-5.5-1.1-7.5-3.1S0,42.2,0,39.3
            c0-2.8,1.1-5.5,3.1-7.5l4.7-4.7c2-2,4.7-3.1,7.5-3.1h2.3c0.6,0,1,0.5,1,1S18.3,26,17.7,26z"
               android:fillColor="@drawable/Gradient1"/>
        </vector>
    

    Gradient1.xml

    <?xml version="1.0" encoding="utf-8" ?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
      <item>
        <shape
         android:shape="rectangle">
          <gradient
             android:angle="45"
             android:startColor="#FF8C77"
             android:endColor="#FF73A4"
             android:type="linear"/>
        </shape>
      </item>
    </selector>
    
Sign In or Register to comment.