Text Color in an Android Custom Renderer

Nathan Sokalski 4,106 Reputation points
2020-11-28T21:48:48.733+00:00

I am working on a CustomRenderer, which involves changing the text color of a TextView. In XAML, I could do this:

android:textColor="@Color /Black"

However, in my CustomRenderer (which uses View/ViewRenderer as base classes), I get errors such as:

cannot convert from 'Xamarin.Forms.Color' to 'Android.Content.Res.ColorStateList?'

for code such as:

this.txtCircleButton.SetTextColor(this.Element.LabelTextColor);

In this statement, this.txtCircleButton is a TextView and LabelTextColor is a Xamarin.Forms.Color. I'm not sure what to do to fix this. Any ideas? Thanks.

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,355 questions
0 comments No comments
{count} votes

Accepted answer
  1. John Hardman 261 Reputation points
    2020-11-29T11:45:38.24+00:00

    Try

    this.txtCircleButton.SetTextColor(this.Element.LabelTextColor.ToAndroid());
    

    The ToAndroid() extension is in the ColorExtensions class in the Xamarin.Forms.Platform.Android namespace.
    There are various overloads depending on your precise requirements.

    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.