Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I bind the TextColor of a TextView?

I am trying to bind the text color of a TextView in Android. Here is my (truncated) xaml:

<TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:MvxBind=" TextColor CategoryTextColor(Category)"/>

where CategoryTextColorValueConverter is as follows:

public class CategoryTextColorConverter : MvxValueConverter<ShowCategory, Color>
{
    protected override Color Convert (ShowCategory value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        if (value == ShowCategory.AllShows)
        {
            return Color.Blue;
        }
        return Color.Red;
    }
}

The converter is getting called and returns a colour as expected, but the text colour never changes on the TextView. I have a similar binding for the background colour that works fine. I've seen here In MvvmCross how do I do custom bind properties that maybe I need to create a custom binding, but I cannot find MvxBaseAndroidTargetBinding. Perhaps I need to install a separate package from nuget?

like image 356
David Conlisk Avatar asked Dec 06 '25 10:12

David Conlisk


2 Answers

The only thing you have to do, is installing the MvvMCross Color Plugin, because TextColor comes with it. It is not included in Core. Your posted solution works.

See: https://github.com/MvvmCross/MvvmCross/wiki/MvvmCross-plugins#color

Tip: You don't have to write platform specific ValueConvertes, if you use MvxColorValueConverter<ShowCategory>, you can share it across different platforms.

public class CategoryTextColorConverter : MvxColorValueConverter<ShowCategory>
{
    protected override MvxColor Convert(ShowCategory value, object parameter, CultureInfo culture)
    {
        if (value == ShowCategory.AllShows)
        {
            return MvxColors.Blue;
        }
        return MvxColors.Red;
    }
}
like image 136
Sven-Michael Stübe Avatar answered Dec 07 '25 22:12

Sven-Michael Stübe


The MVVMCross bindings work by binding to an object's properties and while android:textColor works fine in Android XML it is a shortcut to the object's underlying method, SetTextColor which you can't bind to directly. What you can do is create a class that extends the TextView and have a bindable TextColor property and then use that in your Android XML and bind to that. For example:

public class ExtendedTextView : TextView
{
    public ExtendedTextView(Context context): base (context) { }
    public ExtendedTextView(Context context, IAttributeSet attrs) : base (context, attrs) { }
    public ExtendedTextView(Context context, IAttributeSet attrs, int defStyleAttr) : base(context, attrs, defStyleAttr) { }
    public ExtendedTextView(Context context, IAttributeSet attrs, int defStyleAttr, int defStyleRes) : base (context, attrs, defStyleAttr, defStyleRes) { }
    protected ExtendedTextView(IntPtr javaReference, JniHandleOwnership transfer) : base(javaReference, transfer) { }

    public Color BindableTextColor // property to bind to in XML
    {
        get { return new Color(CurrentTextColor); }
        set { SetTextColor(value);}
    }
}
like image 36
Kevin Ford Avatar answered Dec 08 '25 00:12

Kevin Ford



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!