×

iFour Logo

Incredible App Themes for Xamarin.Forms

Kapil Panchal - December 11, 2020

Listening is fun too.

Straighten your back and cherish with coffee - PLAY !

  • play
  • pause
  • pause
Incredible App Themes for Xamarin.Forms

Predefined and used all over the application is called Application theme. In various popular apps like YouTube, Skype, WhatsApp, Instagram, etc. is providing themes like Light, dark mode to the user. We can change the color of the application using themes by a predefined set of a color scheme like dark, light, or any other color.

We can create a theme using Resources, ResourceDictionary, and Styles in Xamarin Forms. Resources are commonly shared values in the application. It is responsible for the reusability of values that highly scale. It reduces the hard code values in the application, so when we have to change the theme we have to change only a defined set of resources only. We do not have to change all the values.

ResourceDictionary is a group of resources. Resources are stored in ResourceDictionary. In a single ResourceDictionary, we can add multiple resources. We will find ResourceDictionary in App.XAML file.

Styles means assigning various properties to the UI Control for a better look and feel. We can use these defined styles all over the application to give the same look to the application. We do not have to write code on every page for the same control to give the same look in the app we can define a style for that control and bind the style where ever it is needed. Styles are defined in the app. XAML file. Each style has a unique key and target type.

We can define style in App.xaml file in the following way and display in the Xaml page inside the control.

      
	

Applying style to Label

	
	

Let's see the example of applying a theme.

We will be going to create a simple application in which we will apply Light and Dark mode to the application. By default, we will set the theme as Light.

First, we will create a theme using ResourceDictionary and then set a default theme using ResourceDictionary in App.Xaml file.

Code for Light Mode

	
	 White 
	 WhiteSmoke 
	 WhiteSmoke 
	 Black 
	 blue 
	 White 
	 Gray 
	 Transparent 
	
	

CS file

	public partial class LightTheme : ResourceDictionary
	{
	public LightTheme()
	{
	InitializeComponent();
	}
	}
	

Code for Dark Mode

	
	 Black 
	 Gray 
	 Skyblue 
	 White 
	 White 
	 White 
	 WhiteSmoke 
	 Transparent 
	
	

CS File

 
	public partial class DarkTheme : ResourceDictionary
	{
	public DarkTheme()
	{
	InitializeComponent();
	}
	}
	

Now, we will set Light mode as default and add style for UI control we are going to use in our application.

	
	
	
	

Now, we will create View pages that are one for changing the theme and the other two display detail and summary.

This code is for changing the theme of the app, and the second code is a cs file for a select theme.

	
			
	
	

theme1

 

Image: Theme selection page

 

Code for Summary and detail page.

	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	

CS file

	public partial class UserSummaryPage : ContentPage
	{
	public UserSummaryPage()
	{
	InitializeComponent();
	}
	async void OnNavigation(object sender, EventArgs e)
	{
	await Navigation.PushAsync(new UserDetailsPage());
	}
	async void OnThemeToolbarItemClicked(object sender, EventArgs e)
	{
	await Navigation.PushModalAsync(new NavigationPage(new ThemeSelectionPage()));
	}
	}
	

theme2

 

Image: Summary page (Light Mode)

 

theme3

 

Image: Summary (Dark mode)

 

Planning to Hire Xamarin App Development Company? Your Search ends here.

 

Code for detail page

	
	
	
	
	
	
	
	
	
	
	
	        
	
	
	
	
	
	
	
	
	
	
	   
	
	
	
	

 

Cs file

	public partial class UserDetailsPage : ContentPage
	{
	public UserDetailsPage()
	{
	InitializeComponent();
	}
	async void OnThemeToolbarClicked(object sender, EventArgs e)
	{
	await Navigation.PushModalAsync(new NavigationPage(new ThemeSelectionPage()));
	}
	}
	

theme4

 

Image: Detail page (Light Mode)

 

theme5

 

Image: Detail page (Light Mode)

 

Conclusion


In this blog we have learned about what is themes in Xamarin forms and how we can apply to the application. Using ResourceDictionary and Styles we can create and apply themes. We have seen an example in which we have change the theme from light to dark mode dynamically. We have created two ResourceDictionary for both modes and set Light mode as default in app.xaml file.

Incredible App Themes for Xamarin.Forms Predefined and used all over the application is called Application theme. In various popular apps like YouTube, Skype, WhatsApp, Instagram, etc. is providing themes like Light, dark mode to the user. We can change the color of the application using themes by a predefined set of a color scheme like dark, light, or any other color. We can create a theme using Resources, ResourceDictionary, and Styles in Xamarin Forms. Resources are commonly shared values in the application. It is responsible for the reusability of values that highly scale. It reduces the hard code values in the application, so when we have to change the theme we have to change only a defined set of resources only. We do not have to change all the values. ResourceDictionary is a group of resources. Resources are stored in ResourceDictionary. In a single ResourceDictionary, we can add multiple resources. We will find ResourceDictionary in App.XAML file. Styles means assigning various properties to the UI Control for a better look and feel. We can use these defined styles all over the application to give the same look to the application. We do not have to write code on every page for the same control to give the same look in the app we can define a style for that control and bind the style where ever it is needed. Styles are defined in the app. XAML file. Each style has a unique key and target type. We can define style in App.xaml file in the following way and display in the Xaml page inside the control. Applying style to Label Margin="15" Style="{StaticResource LabelStyle2}>Xamarin Let's see the example of applying a theme. We will be going to create a simple application in which we will apply Light and Dark mode to the application. By default, we will set the theme as Light. First, we will create a theme using ResourceDictionary and then set a default theme using ResourceDictionary in App.Xaml file. Code for Light Mode White WhiteSmoke WhiteSmoke Black blue White Gray Transparent CS file public partial class LightTheme : ResourceDictionary { public LightTheme() { InitializeComponent(); } } Read More: Using Eventtocommand Behavior In Mvvm Viewmodel In Xamarin Code for Dark Mode Black Gray Skyblue White White White WhiteSmoke Transparent CS File   public partial class DarkTheme : ResourceDictionary { public DarkTheme() { InitializeComponent(); } } Now, we will set Light mode as default and add style for UI control we are going to use in our application. Now, we will create View pages that are one for changing the theme and the other two display detail and summary. This code is for changing the theme of the app, and the second code is a cs file for a select theme.   Image: Theme selection page   Code for Summary and detail page. CS file public partial class UserSummaryPage : ContentPage { public UserSummaryPage() { InitializeComponent(); } async void OnNavigation(object sender, EventArgs e) { await Navigation.PushAsync(new UserDetailsPage()); } async void OnThemeToolbarItemClicked(object sender, EventArgs e) { await Navigation.PushModalAsync(new NavigationPage(new ThemeSelectionPage())); } }   Image: Summary page (Light Mode)     Image: Summary (Dark mode)   Planning to Hire Xamarin App Development Company? Your Search ends here. See here   Code for detail page   Cs file public partial class UserDetailsPage : ContentPage { public UserDetailsPage() { InitializeComponent(); } async void OnThemeToolbarClicked(object sender, EventArgs e) { await Navigation.PushModalAsync(new NavigationPage(new ThemeSelectionPage())); } }   Image: Detail page (Light Mode)     Image: Detail page (Light Mode)   Conclusion In this blog we have learned about what is themes in Xamarin forms and how we can apply to the application. Using ResourceDictionary and Styles we can create and apply themes. We have seen an example in which we have change the theme from light to dark mode dynamically. We have created two ResourceDictionary for both modes and set Light mode as default in app.xaml file.
Kapil Panchal

Kapil Panchal

A passionate Technical writer and an SEO freak working as a Technical Content Manager at iFour Technolab, USA. With extensive experience in IT, Services, and Product sectors, I relish writing about technology and love sharing exceptional insights on various platforms. I believe in constant learning and am passionate about being better every day.

Build Your Agile Team

Enter your e-mail address Please enter valid e-mail
Categories

Ensure your sustainable growth with our team

Talk to our experts
Sustainable
Sustainable
 

Blog Our insights

Predictive Analytics with ML and .NET
Predictive Analytics with ML and .NET

Many companies rely on advanced technologies to make informed decisions in their business. For instance, some Chief Technology Officers (CTOs) turn to Power BI consulting services,...

C# 8 vs C# 11: Exploring the Key Differences for Business
C# 8 vs C# 11: Exploring the Key Differences for Business

Importance of C# - custom software development C# has been dominating the world of programming for the last two decades. Did you know it is the favored language of about 31%...

Technologies that work well with Angular: A complete guide
Technologies that work well with Angular: A complete guide

Do you know 7 out of 10 people in the world today, use mobile apps for almost everything – from social media, and e-commerce, to healthcare and online transactions? Now you imagine...