Xamarin - working with VectorDrawable in Imageview and Edittext

1.8k views Asked by At

I face issue to apply vectordrawable at my imageView and Edittext

here is my tries to fix the issue

First I am using Xamarin.Android.Support.Vector.Drawable Version 23.4.0

Second I add AppCompatDelegate.CompatVectorFromResourcesEnabled = true; in onCreate of my Application

public class MyApplication : Application
    {


        public MyApplication(IntPtr handle, JniHandleOwnership ownerShip) : base(handle, ownerShip)
        {
            Log.Debug("App", "I am in oncreate application");
        }
        public override void OnCreate()
        {
            base.OnCreate();
            AppCompatDelegate.CompatVectorFromResourcesEnabled = true;

        }

}

*Third * usingAppCompatActivity to make sure my activity used AppCompatImageView instead of normal ImageView and try set drawable manually but it return with null (I dont know why this happen )

 [Activity( Label = "Activity1", Theme = "@style/AppTheme")]
    public class Activity1 : AppCompatActivity
    {
        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);

            SetContentView(Resource.Layout.test);

            ImageView testImage = FindViewById<ImageView>(Resource.Id.imageViewTest);

            Drawable drawable = VectorDrawableCompat.Create(Resources, Resource.Drawable.ic_arrow_back_white_24dp, null);
            testImage.SetImageDrawable(drawable);

        }
    }

Fourth in my project .csproj I add build tools version 23.0.3 <AndroidSdkBuildToolsVersion>23.0.3</AndroidSdkBuildToolsVersion>

here is my full file content

?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="14.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <PropertyGroup>
    <AndroidSdkBuildToolsVersion>23.0.3</AndroidSdkBuildToolsVersion>
    <AndroidDesignerPreferredDevice>Nexus 4</AndroidDesignerPreferredDevice>
    <SelectedDevice>Nexus_5_API_21_x86</SelectedDevice>
    <AndroidDesignerPreferredTheme>Theme.DeviceDefault</AndroidDesignerPreferredTheme>
  </PropertyGroup>
</Project>

Fifth I reference my vector image app:srcCompat

but it still not working any one could help me fix this issue

2

There are 2 answers

0
Mina Fawzy On

I fix first half of my issue Simplly update to

Xamarin.Android.Support.Vector.Drawable Version 23.4.0.1

make sure you reference your vector images by this way app:srcCompat

related to EditText Image I think you cant set it from xml you need to use it via code here is the method

SetCompoundDrawablesWithIntrinsicBounds(Drawable left, Drawable top, Drawable right, Drawable bottom);

in my case I used leftdrawable so my method look like

SetCompoundDrawablesWithIntrinsicBounds(Resource.Drawable.MyDrawable, 0, 0, 0);

hope this help any one face same issue

0
Redwolf On

Update: Forget the Old Solution. Just update to the latest Verison of Xamarin (Xamarin.Android 7.2.0.7) and the latest version of the support libs (25.3.1).


Old Solution:

Downgrade to Xamarin 4.2.0.680, use only the support libs version 23.3.0 (Latest stable 24.2.1 crashes on Android 4.4.4 but works on Android 6)

Also ensure this:

Put this static constructor in your Activity:

static Activity1()
{
   AppCompatDelegate.CompatVectorFromResourcesEnabled = true;
}

Or add AppCompatDelegate.CompatVectorFromResourcesEnabled = true; to your Application class´ OnCreate method.

<AndroidSdkBuildToolsVersion> in your project shouldn't be nessessary anymore.

Use app:srcCompat, like you already do.

Have a look at the Dependencies of each support lib at Nuget

I have:

  • Xamarin.Android.Support.Animated.Vector.Drawable.dll 23.3.0
  • Xamarin.Android.Support.Design.dll 23.3.0
  • Xamarin.Android.Support.v14.Preference.dll 23.3.0
  • Xamarin.Android.Support.v4.dll 23.3.0
  • Xamarin.Android.Support.v7.AppCompat.dll 23.3.0
  • Xamarin.Android.Support.v7.Preference.dll 23.3.0
  • Xamarin.Android.Support.v7.RecyclerView.dll 23.3.0
  • Xamarin.Android.Support.Vector.Drawable.dll 23.3.0

Also make shure you reference Java.Interop.dll

Try out another vector drawable, for example from Material Design Icons (Click on an icon, chose Icon Package, .XML Vector Drawable).