I'm trying to implement Darkmode for Android by using the new AppThemeBinding. It works fine for Android and iOS but i do not have a clue on how to implement it for custom renderers or custom styles.
For example i got a custom Stepper renderer which looks something like this:
protected override void OnElementChanged(ElementChangedEventArgs<Stepper> e)
{
base.OnElementChanged(e);
if (Control != null)
{
Android.Widget.Button buttonDown = (Android.Widget.Button)Control.GetChildAt(0);
Android.Widget.Button buttonUp = (Android.Widget.Button)Control.GetChildAt(1);
if(e.NewElement != null)
{
//Button Down
buttonDown.SetBackgroundResource(Resource.Drawable.button_bg_left);
buttonDown.LayoutParameters = new LinearLayout.LayoutParams(DpToPixel(50), DpToPixel(33));
buttonDown.SetPadding(0,0,0,0);
buttonDown.SetTextColor(Android.Graphics.Color.ParseColor("#007bff"));
//Button Up
buttonUp.SetBackgroundResource(Resource.Drawable.button_bg_right);
buttonUp.LayoutParameters = new LinearLayout.LayoutParams(DpToPixel(50), DpToPixel(33));
buttonUp.SetPadding(0, 0, 0, 0);
buttonUp.SetTextColor(Android.Graphics.Color.ParseColor("#007bff"));
}
}
}
I'm setting a background resource which is a .xml
file and looks like this:
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#FFFFFF"/>
<stroke android:width="1dp" android:color="#007bff" />
<corners
android:topLeftRadius="5dp"
android:bottomLeftRadius="5dp"/>
</shape>
Since the AppThemeBinding is a Markup Extension and works inside .xaml
files, i have no clue how i could implement color changes for my android specific renderers?
How could i go about changing the color of my custom shape for Light/Darkmode?
You can Detect the current system theme in the renderer and then set the corresponding color:
class MyStepperRenderer : StepperRenderer
{
public MyStepperRenderer(Context context) : base(context)
{
}
protected override void OnElementChanged(ElementChangedEventArgs<Stepper> e)
{
base.OnElementChanged(e);
OSAppTheme currentTheme = Xamarin.Forms.Application.Current.RequestedTheme;
if (currentTheme == OSAppTheme.Light)
{
//
}
else
{
//
}
}
}