Harshad Pansuriya
Harshad Pansuriya

Reputation: 20930

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.

Upvotes: 3

Views: 3603

Answers (2)

earthw0rmjim
earthw0rmjim

Reputation: 19417

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))
    );

Upvotes: 5

Ethenyl
Ethenyl

Reputation: 684

Just try :

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

Should work.

Upvotes: 0

Related Questions