Setting Colors And Styles In Application Resources

Introduction

In this article, we are going to learn how to set up a theme or a color for your Xamarin application and change this by using a toggle. Furthermore, we'll see how to use this theme as a default theme for the entire application which will allow shortening the code. And you don’t have to declare the color every time you create a new page.

Targeted Audience

People with a basic knowledge of C# and Xamarin.

Tools

Visual Studio with Xamarin installed.

Whenever you are building an application, you have to set up some basic colors and schemes which you are going to use throughout your application. This technique helps you to remove the duplication of code and makes it easier to apply the modifications by changing the code in only one place. Here we are going to learn how to do this.

The Results will be something like this,

Setting Colors And Styles In Application Resources
 
Setting Colors And Styles In Application Resources 

In the pictures above you can clearly see that the toggle is off by default and the background is set to black, but when the toggle is switched to on the color turns white. Let's see how to do this.

To set the main colors, simply go to App.xaml and put a tag name <Application.Resources> between the <Application> tag. It contains all the resources of your application like coloring and styling. It further consists of <ResourceDictionary> in which you are going to declare your colors and styles. So in this, simply put your color tags. For now, I’m using black and white only.

XAML code for App.xaml.
  1. <?xml version="1.0" encoding="utf-8" ?>  
  2. <Application xmlns="http://xamarin.com/schemas/2014/forms"  
  3.              xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"  
  4.              x:Class="PracApp1.App">  
  5.     <Application.Resources>  
  6.         <ResourceDictionary>  
  7.             <Color x:Key="LabelColor">#FFFFFF</Color>  
  8.             <Color x:Key="BgColor">#000000</Color>  
  9.             <Style x:Key="ThemeColor" TargetType="StackLayout">  
  10.                 <Setter Property="BackgroundColor" Value="{x:DynamicResource BgColor}"></Setter>  
  11.             </Style>  
  12.         </ResourceDictionary>  
  13.     </Application.Resources>  
  14. </Application>  

You can see I declared two color tags; one is for my label and the second is for my default background theme color. Bgcolor is bound with the style tag which means the style will contain the black color. Style tag consists of property name “TargetType” which denotes what type of layout it is targeting.

Setter tag contains a property which will show where it is going to apply and value is bound with the BfColor.

XAML Code for Main Page,
  1. <StackLayout Style="{x:DynamicResource ThemeColor}">  
  2.         <Label TextColor="{x:DynamicResource LabelColor}" HorizontalOptions="Center" VerticalOptions="Center" Text="Saleh Qadeer"></Label>  
  3.         <Switch   
  4.             BackgroundColor="{x:DynamicResource LabelColor}"   
  5.             HorizontalOptions="Center" VerticalOptions="Center"   
  6.             Toggled="Switch_Toggled"></Switch>  
  7.     </StackLayout>  

Simply put a stack layout and I also used a label and a switch in it. The layout is bound with the ThemeColor and label and switch are bound with the LabelColor. Switch toggle event is created and now I’m going to show how to change the color on the toggle. Simply use this code in the MainPage.xaml.cs

C# Code
  1. private void Switch_Toggled(object sender, ToggledEventArgs e)  
  2.         {  
  3.             var getVal = sender as Switch;  
  4.             if (getVal.IsToggled)  
  5.             {  
  6.                 App.Current.Resources["LabelColor"] = "#000000";  
  7.                 App.Current.Resources["BgColor"] = "#FFFFFF";  
  8.             }  
  9.             else  
  10.             {  
  11.                 App.Current.Resources["LabelColor"] = "#FFFFFF";  
  12.                 App.Current.Resources["BgColor"] = "#000000";  
  13.             }  
  14.         }  

The code shows when the switch is toggled the Label and switch turnblack and the background color turns white. This all happens due to the App.Current.Resources which helps you to access the current resources of your application.

Summary

Having some knowledge about application resources helps you a lot in defining the resources of your application. You can set different images to the background or can declare basic and major colors of the app. So this article helps you to learn about  app resources and teaches you how to set them and change them if needed.


Similar Articles