C# - Resources.GetDrawable is obsolete in Android Xamarin

10,842

Solution 1

Change to SetImageResource like this

currPaint.SetImageResource(Resource.Drawable.paint_pressed);

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);
Share:
10,842
Jake Muller
Author by

Jake Muller

Updated on June 22, 2022

Comments

  • Jake Muller
    Jake Muller almost 2 years

    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:

    SetContentView(Resource.Layout.Main);
    drawView = FindViewById<DrawingView>(Resource.Id.drawing);
    LinearLayout paintLayout = FindViewById<LinearLayout>(Resource.Id.paint_colors);
    currPaint = (ImageButton)paintLayout.GetChildAt(0);
    currPaint.SetImageDrawable(Resources.GetDrawable(Resource.Drawable.paint_pressed));
    

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

    Android.Content.Res.Resources+NotFoundException

    '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);
    //or
    ContextCompat.getDrawable(getActivity(), R.drawable.name);
    //or
    ResourcesCompat.getDrawable(getResources(), R.drawable.name, anotherTheme);
    

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