4
votes

I am creating an Android application in Xamarin Forms. I am trying to add a ToolbarItem icon using some of the examples from this forum using XAML like so:

<ToolbarItem Name="Add" Activated="OnAddClick"
           Priority="1" Order="Primary" Icon="Create.png" />

My Create.png icon file is located in /Resources/drawable/ located in the Android project. I have done the same thing in Android Studio using a menu layout file and my icons are visible. I am, however, struggling to accomplish the same result in Xamarin Forms. Is this a problem with Xamarin Forms or am I doing something wrong. Any suggestions?

1
Try renaming your image to all lowercase and no special characters and try again. Android has restrictions on the filenames that are allowed for resources. In order to work on all platforms it must be valid for all platforms. - Gerald Versluis
Yep, now it's working. I'm kinda wondering whether the folks on the forum I mentioned are aware of this because some of their examples had upper-case letters in their file names. Thanks Gerald! - Razor
Let me turn it into an answer for you :) - Gerald Versluis
After using Android Studio for more than two years, I still make those silly mistakes. The good thing about Android Studio is it underlines the file which contains illegal characters. - Razor

1 Answers

3
votes

In order for this to work truly cross-platform, make sure the filename is compliant with all of the requirements for each platform. Specifically Android is known to be picky. Just only lowercase filenames without any special characters. You can only use lower-case letters, numbers, underscores and periods.

Example valid filename is create.png or icon_create.png.

Non-valid examples are: Create.png, icon create.png, create-icon.png, etc.

Read more on the Xamarin documentation page.

As pointed out in the comments, Xamarin Studio does an excellent job of pointing out the illegal filenames.