I have a Xamarin Forms application which gives the user 3 theme options. I want to be able to change the Tabbar background, selected item and unselected item color with a button click. In iOS I was able to do this with a renderer like below:
protected override void OnElementChanged(VisualElementChangedEventArgs e)
{
base.OnElementChanged(e);
if(e.OldElement != null)
{
Xamarin.Forms.Application.Current.PropertyChange -= Current_PropertyChanged;
return;
}
Xamarin.Forms.Application.Current.PropertyChange += Current_PropertyChanged; //subscribe to the App class' built in property changed event
UpdateTheme();
}
void Current_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
{
UpdateTheme();
}
In Android, I know its possible to change these colors in styles.xml
but that would only allow me to set the colors once. Also, I am using the ToolbarPlacement="Bottom"
to place my tab bar at the bottom of the screen.
android:TabbedPage.ToolbarPlacement="Bottom"
android:TabbedPage.BarSelectedItemColor="Red"
android:TabbedPage.IsSwipePagingEnabled="False"
I wonder if its possible to be able to change the BarSelectedItemColor
dynamically with a button click.
I have finally made this working by using DynamicResource
styling:
From this:
android:TabbedPage.BarSelectedItemColor="Red"
To this:
android:TabbedPage.BarSelectedItemColor="{DynamicResource BarSelectedItemColor"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With