Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to set icon for Toolbar Item in Xamarin forms

My xamarin andorid project does not have Drawawble folders under Resources, I have mipmap folders instead. I'm trying to set icon for a toolbar item in shared project. If I set an image as embeded resource I should be able to access it from the shared project, am I wrong?

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" 
x:Class="BoringAppUi.MainPage" Title="Main Page">
    <ContentPage.ToolbarItems>
        <ToolbarItem Text="Logout" Clicked="OnLogoutButtonClicked" Order="Primary"  Priority="0"/>
        <ToolbarItem Text="Home" Icon="@mipmap/baseline_home_blue_48.png" Clicked="OnHomeIconClicked" Order="Primary" Priority="1"/>
    </ContentPage.ToolbarItems>
    <ContentPage.Content>
        <StackLayout>
            <Label Text="Main app content goes here" HorizontalOptions="Center" VerticalOptions="CenterAndExpand" />
        </StackLayout>
    </ContentPage.Content>
</ContentPage>

Also I've tried setting it from the code behind

var toolbarItemHome = new ToolbarItem
{
    Text = "Home",
    Icon = "@mipmap/baseline_home_blue_48.png"
};
toolbarItemHome.Clicked += OnHomeIconClicked;
ToolbarItems.Add(toolbarItemHome);
like image 557
3not3 Avatar asked Aug 14 '18 14:08

3not3


People also ask

How to add Toolbar item in Xamarin Forms?

The Xamarin. Forms ToolbarItem class is a special type of button that can be added to a Page object's ToolbarItems collection. Each ToolbarItem object will appear as a button in the application's navigation bar. A ToolbarItem instance can have an icon and appear as a primary or secondary menu item.

What is Shell in xamarin forms?

Xamarin. Forms Shell reduces the complexity of mobile application development by providing the fundamental features that most mobile applications require, including: A single place to describe the visual hierarchy of an application. A common navigation user experience.


1 Answers

From c# code:

new ToolbarItem () { Icon = "icon.png"}

Or from xaml:

<ToolbarItem Name="iconexample" Icon="icon.png" Priority="0" Order="Primary" Activated="Onclick" />  

Have that in mind:

iOS - place the image at /mipmap/icon.png

Android - place the image at /mipmap/drawable/icon.png

like image 94
michaelitoh Avatar answered Nov 06 '22 12:11

michaelitoh