Search code examples
androidxamarinmvvmcross

MvvmCross convert bool to drawable in Android


In my Xamarin Android app I'm trying to use a converter on a boolean to highlight the selected item in a list.

Does anyone know if it is even possible to use a converter on a boolean to select one or another drawable as the background of a LinearLayout ?

I feel like I'm missing something. I've tried returning various types from my converter, but nothing seems to work.

 <LinearLayout
    android:orientation="vertical"
    android:layout_width="wrap_content"
    local:MvxBind="Background IsSelected, Converter=BoolToDrawable"
    android:layout_height="wrap_content">

Solution

  • I have done something similar, I have an int (RowItem.SummaryEnumPlayersInt) which I convert to a drawable:

    <TextView
        style="@style/TeamDifficulty"
        android:layout_width="15dp"
        android:layout_height="15dp"
        android:gravity="center"
        android:background="@drawable/background_circle"
        local:MvxBind="Text RowItem.SummaryEnumPlayersInt; Summary RowItem.SummaryEnumPlayers" />
    

    Notice the custom "Summary" binding, this is where the magic happens, in setup:

    protected override void FillTargetFactories(IMvxTargetBindingFactoryRegistry registry)
    {
        base.FillTargetFactories(registry);
    
        registry.RegisterFactory(new MvxCustomBindingFactory<TextView>("Summary", textView => new SummaryTextViewBinding(textView)));
    }
    

    And here is that custom binding class:

    public class SummaryTextViewBinding : MvxAndroidTargetBinding
    {
        readonly TextView _textView;
        SummaryEnumeration _currentValue;
    
        public SummaryTextViewBinding(TextView textView) : base(textView)
        {
            _textView = textView;
        }
    
        #region implemented abstract members of MvxConvertingTargetBinding
        protected override void SetValueImpl(object target, object value)
        {            
            if (!string.IsNullOrEmpty(_textView.Text))
            {
                _currentValue = (SummaryEnumeration)Convert.ToInt32(_textView.Text);
    
                SetTextViewBackground();
            }
        }
        #endregion
    
        void SetTextViewBackground()
        {
            switch (_currentValue)
            {
                case SummaryEnumeration.Easy:
                    _textView.SetBackgroundResource(Resource.Drawable.background_circle_green);
                    _textView.Text = string.Empty;
    
                    break;
                case SummaryEnumeration.Medium:
                    _textView.SetBackgroundResource(Resource.Drawable.background_circle_yellow);
                    _textView.Text = string.Empty;
    
                    break;
                case SummaryEnumeration.Difficult:
                    _textView.SetBackgroundResource(Resource.Drawable.background_circle_red);
                    _textView.Text = string.Empty;
    
                    break;
                case SummaryEnumeration.None:
                    _textView.SetBackgroundResource(Resource.Drawable.background_circle_none);
                    _textView.Text = LocalizationConstants.Nothing;
    
                    break;
            }
        }
    
        public override Type TargetType
        {
            get { return typeof(bool); }
        }
    
        public override MvxBindingMode DefaultMode
        {
            get { return MvxBindingMode.OneWay; }
        }
    }
    

    Enjoy!