James Montemagno
James Montemagno

Live, Love, Bike, and Code.

Live, Love, Bike, and Code



Xamarin.Forms - Icons in Navigation Toolbar

A somewhat common scenario for mobile apps is to place an icon in the center of the Navigation Toolbar. While I am not sure if this is really best practice in 2018 there still seems to be a lot of demand to do this. What if I was to tell you that this is actually pretty easy to do and for iOS it is built directly into Xamarin.Forms with no need for a custom renderer?

Let's take the default "Master/Detail" template for Xamarin.Forms in Visual Studio. It has a few tabs and an about page that has the Xamarin logo in it. Let's say we want to put that logo into the Toolbar for some reason, let's do it!

Xamarin.Forms 3.2.0 Updates!

The release of Xamarin.Forms 3.2.0 brought some amazing new updates for the Navigation Toolbar including the new TitleView that allows you to set any custom content you would like! Here is the new fancy way of doing it:

        <StackLayout Orientation="Horizontal" VerticalOptions="Center" Spacing="10">
            <Image Source="iconXamagon.png">
                        Tapped="HandleTapped" />
            <Label Text="3.2.0" FontSize="16" TextColor="Black" VerticalTextAlignment="Center" />

On an older version of Xamarin.Forms? Here is the original post:

iOS Toolbar Icon

This is the easiest as it is built directly in to the API and applied on a Page by Page basis. That is right you can easily adjust the icon that is displayed by setting the TitleIcon static property on NavigationPage.

So if we want our icon to appear on the first tab, we can set the property:

<views:ItemsPage NavigationPage.TitleIcon="xamarin_logo.png"/>

This of course needs to be inside of a Navigation page, so here is the full XAML:

<TabbedPage xmlns="http://xamarin.com/schemas/2014/forms"
        <NavigationPage Title="Items">
                <OnPlatform x:TypeArguments="FileImageSource">
                    <On Platform="iOS" Value="tab_feed.png"/>
                <!-- Set Title Icon Here-->
                <views:ItemsPage NavigationPage.TitleIcon="xamarin_logo.png"/>
        <!-- About Page-->

Just like that we have a beautiful (although not properly sized) logo in our navigation toolbar:

Icon on items page for iOS

You can also set it in the code behind for the specific page inside of the constructor if you don't want to clutter up your XAML.

NavigationPage.SetTitleIcon(this, "xamarin_logo.png");

This will yield the same result. Ensure that this icon is inside of your iOS project's Resources and is marked as a BundleResource. You should also follow Apple's guidelines for the size of this icon.

Android Toolbar Icon

Unfortunately the TitleIcon property only works for iOS :(, but we do have the ability to set the icon in our toolbar on a global level for the application. Inside of the Android projects Resources/layout folder lives Toolbar.axml and this is what you set in the MainActivity, which Xamarin.Forms will use to inflate it. Double click to open up the Toolbar.axml and head over to the Source view. The toolbar can actually have anything inside of it, which is pretty nice. We could add a LinearLayout and stack multiple items or we can add an ImageView directly:

        android:layout_gravity="center" />

This will result in any NavigationPage in your app to have a fancy icon on it. You can even add padding to adjust it.

Android App with Icon in Toolbar

Hopefully Xamarin.Forms adds an ability to just create a Toolbar for any page that we are on, so we can have this on a page by page basis on Android. The other option here would be to hide the actual Toolbar on Android and just create our own from scratch. This has some up sides as you have more control, but the toolbar won't react to dpi and rotation changes unless you implement more code to do so.

Learn More

The SetTitleIcon property is actually documented over on docs.microsoft.com.

Copyright © James Montemagno 2018 All rights reserved. Privacy Policy

View Comments