Adding Platform-Specific Images And Icons In Application

Introduction

Sometimes you need to work with images that are specific to each platform like icons, buttons, and toolbars because each of the platforms has its own designed guideline. So, if you want to give your application a native look and feel on each platform, you want to use platform-specific images. In this article, you are going to learn how to add a platform-specific icon to a button in your application.

Targeted Audience

People with the basic knowledge of C# and Xamarin.

Tools

Visual Studio with Xamarin installed.

To download a perfect icon for the app, I am using the website https://icons8.com. You can find different types of icons from here, and each of them is free to use. You can also customize them or get them in different sizes and colors.

Image for iOS

Both iOS and Android are capable of loading different resolution images based on the targeted device. So, you probably know that the newer phones have more pixel density which means you can display higher-quality images. That’s why inside the iOS folder, we have images of different resolutions.

Adding Platform Specific Images And Icons In Application 

The thing to note here is the naming convention for iOS. Our original file is 32x32 pixels. We have another clock file with the suffix at 2x which is twice as big as the original clock file. And similarly, we have another file with the suffix at 3x. So, whenever you want to supply an image to iOS, it's best to supply the image in three different sizes.

To add the images, go to your iOS project and right-click on the “Resources” folder to add files. Select all the three images and add them into the folder.

Adding Platform Specific Images And Icons In Application

 
Image for Android

In Android, we have a different convention. So we have different folder names and all these folders start with “drawable”. Here, hdpi stands for High DPI, xhdpi stands for Extra High DPI, xxhdpi stands for Extra Extra High DPI image and xxxhdpi stands for Extra Extra Extra High DPI image.

Adding Platform Specific Images And Icons In Application

To add files in Android, we also have a folder named “Resources” and inside that folder, we again have subfolders. Simply place each density image to the corresponding folder.

Adding Platform Specific Images And Icons In Application 
 

Image for Windows

In Windows, we have only one file inside the Windows folder. You can place the image in the root of the project; it doesn’t need to be placed in any folder.

Adding Platform Specific Images And Icons In Application 

Now, let’s add the image to the button. You can add that using the XAML code.

XAML Code
  1. <?xml version="1.0" encoding="utf-8" ?>  
  2. <ContentPage xmlns="http://xamarin.com/schemas/2014/forms"  
  3.              xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"  
  4.              xmlns:local="clr-namespace:PracApp1"  
  5.              x:Class="PracApp1.MainPage"   
  6.              x:Name="ContentPage1">  
  7.     <AbsoluteLayout VerticalOptions="Center" HorizontalOptions="Center">  
  8.         <Button x:name="Btn" Image="clock.png"></Button>  
  9.   
  10.     </AbsoluteLayout>  
  11. </ContentPage>  

You can also add this by using C# Code.

C# Code
  1. public MainPage()  
  2.         {  
  3.             InitializeComponent();  
  4.             Btn.Image = (FileImageSource)ImageSource.FromFile("clock.png");  
  5.         }  

The result will be something like this.

Adding Platform Specific Images And Icons In Application 

To add the platform-specific image, you can use this code. The results will be the same.

C# Code
  1. Btn.Image = (FileImageSource)ImageSource.FromFile(Device.OnPlatform(  
  2.                 iOS: "clock.png",  
  3.                 Android: "clock.png",  
  4.                 WinPhone: "clock.png"  
  5.                 ));  

If you want to do this from XAML, you can write the following code.

XAML Code
  1. <Button>  
  2.             <Button.Image>  
  3.                 <OnPlatform x:TypeArguments="FileImageSource" iOS="clock.png" Android="clock.png" WinPhone="clock.png"></OnPlatform>  
  4.             </Button.Image>  
  5.         </Button>  

That is it.

This article gave a brief introduction of how to add icons and platform-specific images to your application. Hope this helps you throughout adding platform-specific images.


Similar Articles