Forum Xamarin Xamarin.Forms

Xamarin.Forms: How to add logo to the navigation bar on Android?

I achieve to add a logo with a custom navigation renderer on iOS. However, I have some troubles with the Android version. Here are my custom renderer codes for Android:

using Xamarin.Forms.Platform.Android;
using Xamarin.Forms;
using My.app.Droid;

[assembly: ExportRenderer(typeof(NavigationPage), typeof(CustomNavigationPage))]
namespace My.app.Droid
{
    public class CustomNavigationPage : NavigationRenderer
    {
        protected override void OnElementChanged(ElementChangedEventArgs<NavigationPage> e)
        {
            base.OnElementChanged(e);

            var actionBar = ((Activity)Context).ActionBar;
            actionBar.SetIcon(Resource.Drawable.logo);
        }
    }
}

It gives these errors:

1-

Warning CS0618: 'NavigationRenderer.NavigationRenderer()' is obsolete: 'This constructor is obsolete as of version 2.5. Please use NavigationRenderer(Context) instead.' (CS0618)

2-

Error CS0246: The type or namespace name 'Activity' could not be found (are you missing a using directive or an assembly reference?) (CS0246)

I need to have this structure:

Best Answer

Answers

  • AlessandroCaliaroAlessandroCaliaro ITMember ✭✭✭✭✭

    I suggest you to upgrade to XF 3.2 and use TitleView https://developer.xamarin.com/samples/xamarin-forms/Navigation/TitleView/

  • XF version is already set to 3.2.

    However, I cannot use <NavigationPage.TitleView> tag in this scenario:

    <?xml version="1.0" encoding="utf-8"?>
    <MasterDetailPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:local="clr-namespace:My.app" x:Class="My.app.MainPage">
        <MasterDetailPage.Master>
            <ContentPage Title="Menu" BackgroundColor="#0a82b8" Icon="menu.png">
                <StackLayout Orientation="Vertical">
                    <ListView x:Name="navigationDrawerList" RowHeight="55" SeparatorVisibility="None" BackgroundColor="#ffffff" ItemSelected="OnMenuItemSelected">
                        <ListView.ItemTemplate>
                            <DataTemplate>
                                <ViewCell>
                                    <!-- Main design for our menu items -->
                                    <StackLayout VerticalOptions="FillAndExpand" Orientation="Horizontal" Padding="20,10,0,10" Spacing="20">
                                        <Label Text="{Binding Title}" FontSize="Large" VerticalOptions="Start" HorizontalOptions="CenterAndExpand" TextColor="#28DDFF" FontAttributes="Bold" />
                                    </StackLayout>
                                </ViewCell>
                            </DataTemplate>
                        </ListView.ItemTemplate>
                    </ListView>
                </StackLayout>
            </ContentPage>
        </MasterDetailPage.Master>
        <MasterDetailPage.Detail>
            <NavigationPage.TitleView>
                <Label Text="My TitleView"/>
            </NavigationPage.TitleView>
        </MasterDetailPage.Detail>
    </MasterDetailPage>
    

    The output of the debug:

    Type NavigationPage.TitleView not found in xmlns http://xamarin.com/schemas/2014/forms

  • AlessandroCaliaroAlessandroCaliaro ITMember ✭✭✭✭✭
    Have you tried the sample i sent to you?
  • MadameGreenPeaMadameGreenPea Member ✭✭
    Accepted Answer

    Thank you for your suggestion.

    I added the logo by following this way:
    https://stackoverflow.com/a/42181176/9905979

Sign In or Register to comment.