C# - Resources.GetDrawable is obsolete in Android Xamarin


Solution 1

Change to SetImageResource like this


Solution 2

var currPaint= ContextCompat.GetDrawable(this,Resource.Drawable.paint_pressed);
        currPaint.SetBounds(0, 0, 60, 60);

This is what i have implimented in my project even i had same problem.Hope this helps! @Jake

Solution 3

(for a Xamarin Forms project)

When I need to get a drawable from my .Droid project I usually use the following line:

Control.Background = Context.GetDrawable(Resource.Drawable.ResourceName);
Jake Muller
Author by

Jake Muller

Updated on June 22, 2022


  • Jake Muller
    Jake Muller 11 months

    I implemented Resources.GetDrawable in andrdoid Xamarin. The program works, but if I clicked the button implementing the Resources.GetDrawable the program force close. Here's my code:

    drawView = FindViewById<DrawingView>(Resource.Id.drawing);
    LinearLayout paintLayout = FindViewById<LinearLayout>(Resource.Id.paint_colors);
    currPaint = (ImageButton)paintLayout.GetChildAt(0);

    The Resources.GetDrawable(Resource.Drawable.paint_pressed got green underlined(Xamarin in Visual Studio 2015). The log message returned exception:


    'Resources.GetDrawable(int)' is obsolete: 'deprecated'

    For Java version the solution is by using one of these 3:

    ResourcesCompat.getDrawable(getResources(), R.drawable.name, null);
    ContextCompat.getDrawable(getActivity(), R.drawable.name);
    ResourcesCompat.getDrawable(getResources(), R.drawable.name, anotherTheme);

    What is the version for C# Android Xamarin?, Thank you.