How to customize arrow icon, page icon and page title in MasterDetailPage - Xamarin.Forms
Asked Answered
L

1

10

I have created a new Blank App (Xamarin.Forms Portable) project in Visual Studio 2015 and modified App.cs to get "hamburger menu":

public class App : Application
{
    public App()
    {
        var masterPage = new ContentPage()
        {
            Content = new Label { Text = "Hello from Master!"},
            Title = "Master Page"
        };

        var detailPage = new ContentPage()
        {
            Content = new Label { Text = "Hello from Detail!" },
            Title = "Detail Page"
        };

        var mainPage = new MasterDetailPage()
        {
            Master = masterPage,
            Detail = detailPage,
            Title = "Main Page"
        };

        // The root page of your application
        MainPage = mainPage;
    }
    . . .
}

Everything works fine, but how can I customize these four things:

1) Hide / change Arrow

2) Hide / change Icon

3) Hide / change Title text

4) Hide whole toolbar

MasterDetailPage

Lucretialucretius answered 29/7, 2015 at 19:2 Comment(0)
F
12
  1. You can change arrow to hamburger icon if you use your DetailPage within NavigationPage:

    Detail = new NavigationPage(detailPage);
    
  2. To change icon, just change project files:

    • YourProject/Resources/drawable/icon.png
    • YourProject/Resources/drawable-hdpi/icon.png
    • YourProject/Resources/drawable-xhdpi/icon.png
    • YourProject/Resources/drawable-xxhdpi/icon.png

    or on your MasterDetailPage set Icon property to another resource.

    If you want to hide icon - it only applies to Android. It can be solved with custom renderer (http://developer.xamarin.com/guides/cross-platform/xamarin-forms/custom-renderer/):

    public class CustomNavigationRenderer : NavigationRenderer
    {
        protected override void OnElementChanged(ElementChangedEventArgs<NavigationPage> e)
        {
            base.OnElementChanged (e);
    
            var actionBar = ((Activity)Context).ActionBar;
            actionBar.SetIcon (Resource.Color.transparent);
        }
    }
    

    EDIT: It can also be done in MainActivity.cs:

    ActionBar.SetIcon (new ColorDrawable(Resources.GetColor (Android.Resource.Color.Transparent)));
    
  3. Just use Title property on Page.

  4. SetHasNavigationBar(page, false);

Fjord answered 30/7, 2015 at 14:54 Comment(3)
But i haven't found Resources folder in portable project, but it is available in xamarinMyProject.DroidJog
I used your code and got null value for actionBar. What I did wrong?Cirrus
@JosephKatzman If your MainActivity extends from FormsAppCompatActivity then you need to use SupportActionBar instead of ActionBar.Giusto

© 2022 - 2024 — McMap. All rights reserved.