Ironman Ironman - 24 days ago 13
Android Question

SetBackgroundColor for Button from Resource file in Xamarin (Android)

I want to set the background color of a

Button
. I am using Visual Studio with Xamarin.

In Android we use:

Java code :

button_vstrong_fluorescence.setBackgroundColor(ContextCompat.getColor(InventoryActivity.this, R.color.linear_filter_background));


but In Xamarin
C# code.

button_vstrong_fluorescence.SetBackgroundColor(Android.Graphics.Color.ParseColor("#EBECEC"));


In C# the upper code is work fine.

But every time i want right this thing static way

Android.Graphics.Color.ParseColor("#EBECEC")


Is there any way I can set
#EBECEC
this color from my Resource.

the
Color.ParseColor
require
String
value so .

What I Try :



Android.Graphics.Color.ParseColor(Resource.Colors.linear_filter_background.ToString);


But it give me below error.

enter image description here

Is there any way to set Background color from Resource??

Any help be Apreciated.

Answer

Not an expert in Xamarin (or C#) but something like this should work to get your color resource:

button_vstrong_fluorescence
    .SetBackgroundColor(new Android.Graphics.Color(
        ContextCompat.GetColor(this, Resource.Color.linear_filter_background))
    );
Comments