Search code examples
c#androidxamarinparcel

Connot convert from 'int' Android.OS.Parcel


I am trying to use spans to change the color of parts parts of my text textviews. But I keep getting this strange error, like my colors aren't recognized.

this is my code

        var span2 = new SpannableString(beforeMisterX + misterX);
        span2.SetSpan(new ForegroundColorSpan(ContextCompat.GetColor(this, Resource.Color.abc_hint_foreground_material_dark)), 15, 6, 0);
        lblMisterX.SetText(span2, TextView.BufferType.Spannable);

The error is at this line:

span2.SetSpan(new ForegroundColorSpan(ContextCompat.GetColor(this, Resource.Color.abc_hint_foreground_material_dark)), 15, 6, 0);

Screenshot of error

I also tried this but i still get the same error:

span.SetSpan(new ForegroundColorSpan(Color.HoloBlueDark), 15, 6, 0);

Solution

  • ForegroundColorSpan expects a Color, not an int, Color.HoloBlueDark and the return from ContextCompat.GetColor are ints, so you need to convert it to a Color:

    var color = new Android.Graphics.Color(ContextCompat.GetColor(this, Resource.Color.abc_hint_foreground_material_dark));
    span2.SetSpan(new ForegroundColorSpan(color), 15, 6, 0);