0

I have created custom bordered stacklayout for android using custom renderer. The StackLayout border is created by using xml file. The border color is defined in that xml file. But I want to change that border color dynamically at runtime using bindable object property. I have done this with ios. But i dont have any idea how to bind the bindable object to the xml file. My customBorderStacklayout sample code is mentioned below, Please share your valuable suggestion to done this.

CustomStackBorder.cs

public class CustomStackBorder : StackLayout
{
    public Color BorderColor
    {
        get { return (Color)GetValue(BorderColorProperty); }
        set { SetValue(BorderColorProperty, value); }
    }

    public static readonly BindableProperty BorderColorProperty =
            BindableProperty.Create("BorderColor", typeof(Color), typeof(CustomStackBorder), Color.Gray, BindingMode.TwoWay);

    public CustomStackBorder()
    {       
    }
}

CustomStackLayoutRenderer.cs (Android)

public class CustomStackLayoutRenderer : VisualElementRenderer<StackLayout>
{
      public CustomStackLayoutRenderer(Context context) : base(context)
      {
      }
      protected override void OnElementChanged(ElementChangedEventArgs<StackLayout> e)
      {
           base.OnElementChanged(e);
           Background = ContextCompat.GetDrawable(this.Context, Resource.Drawable.StackLayoutBorder);            
      }
}

StackLayoutBorder.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
  <stroke android:width="0.1dp" android:color="#ff555555"></stroke>
  <corners android:topLeftRadius="2dp"
            android:topRightRadius="2dp"
            android:bottomLeftRadius="2dp"
            android:bottomRightRadius="2dp" />
<solid android:color="#ffffff"/>
</shape>

1 Answer 1

1

You can do it by setting the stroke color of the drawable.

Code:

protected override void OnElementChanged(ElementChangedEventArgs<StackLayout> e)
    {
        base.OnElementChanged(e);

        if (e.NewElement != null)
        {
            if (e.NewElement as CustomStackBorder != null)
            {
                Background = ContextCompat.GetDrawable(this.Context, Resource.Drawable.StackLayoutBorder);
                GradientDrawable bgShape = (GradientDrawable)this.Background;
                bgShape.SetStroke(1, (e.NewElement as CustomStackBorder).BorderColor.ToAndroid());

            }
        }
    }

Change your OnElementChanged method in custom stack view renderer with my code and replace your BorderColor property value with real data. That is all you have to do.

Happy Coding!

Sign up to request clarification or add additional context in comments.

2 Comments

I have tried your code but it will return the following exception, Unhandled Exception: Java.Lang.IllegalArgumentException: Unknown color
@Yogeshwaran, I have updated my code. Please have a look.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.