Hello,
Welcome to our Microsoft Q&A platform!
Sorry for my late response. We have been able to change the background color of visual elements (such as Frame, StackLayout etc) using custom renderer.
Same concept can be applied to other visual elements that are not working out-of-the-box with Xamarin Forms. You just need to access the Android APIs which can be done by using a renderer.
LabelRenderer.cs
using Android.Content;
using VisualFocusApp.Droid;
using Xamarin.Forms;
using Xamarin.Forms.Platform.Android.FastRenderers;
[assembly: ExportRenderer(typeof(Label), typeof(MyLabelRenderer))]
namespace VisualFocusApp.Droid
{
public class MyLabelRenderer : LabelRenderer
{
public MyLabelRenderer(Context context) : base(context)
{
}
protected override void OnElementChanged(Xamarin.Forms.Platform.Android.ElementChangedEventArgs<Label> e)
{
base.OnElementChanged(e);
if (Control != null)
{
Control.FocusChange += Control_FocusChange;
}
}
private void Control_FocusChange(object sender, FocusChangeEventArgs e)
{
if (Control.HasFocus)
{
Control.SetBackgroundColor(Android.Graphics.Color.Red);
}
else
{
Control.SetBackgroundColor(Android.Graphics.Color.White);
}
}
}
}
CustomStackLayoutRenderer.cs
using Android.Content;
using VisualFocusApp.Droid;
using Xamarin.Forms;
using Xamarin.Forms.Platform.Android;
[assembly: ExportRenderer(typeof(StackLayout), typeof(CustomStackLayoutRenderer))]
namespace VisualFocusApp.Droid
{
public class CustomStackLayoutRenderer : VisualElementRenderer<StackLayout>
{
public CustomStackLayoutRenderer(Context context) : base(context)
{
}
protected override void OnElementChanged(ElementChangedEventArgs<StackLayout> e)
{
base.OnElementChanged(e);
if (this != null)
{
FocusChange += Control_FocusChange;
}
}
private void Control_FocusChange(object sender, FocusChangeEventArgs e)
{
if (HasFocus)
{
SetBackgroundColor(Android.Graphics.Color.Red);
}
else
{
SetBackgroundColor(Android.Graphics.Color.White);
}
}
}
}
CustomFrameRenderer.cs
using Android.Content;
using VisualFocusApp.Droid;
using Xamarin.Forms;
using Xamarin.Forms.Platform.Android.FastRenderers;
[assembly: ExportRenderer(typeof(Frame), typeof(CustomFrameRenderer))]
namespace VisualFocusApp.Droid
{
public class CustomFrameRenderer : FrameRenderer
{
public CustomFrameRenderer(Context context) : base(context)
{
}
protected override void OnElementChanged(Xamarin.Forms.Platform.Android.ElementChangedEventArgs<Frame> e)
{
base.OnElementChanged(e);
if (Control != null)
{
Control.FocusChange += Control_FocusChange;
}
}
private void Control_FocusChange(object sender, FocusChangeEventArgs e)
{
if (Control.HasFocus)
{
Control.SetBackgroundColor(Android.Graphics.Color.Red);
}
else
{
Control.SetBackgroundColor(Android.Graphics.Color.White);
}
}
}
}
We will look into iOS, it doesn’t work out of the box like android, then it might take a while to figure it out.
Best Regards,
Wenyan Zhang
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.