Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to change the tabbar selected item color dynamically in Xamarin Forms Android?

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.

like image 560
iamsophia Avatar asked Oct 20 '18 13:10

iamsophia


1 Answers

I have finally made this working by using DynamicResource styling:

From this:

android:TabbedPage.BarSelectedItemColor="Red"

To this:

android:TabbedPage.BarSelectedItemColor="{DynamicResource BarSelectedItemColor"
like image 124
iamsophia Avatar answered Oct 28 '22 11:10

iamsophia