Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xamarin.Forms custom Android NavigationPageRenderer title and subtitle

Currently working on a project where I want to use AppCompat and have a requirement setting title and subtitle on most of the pages.

It doesn't work using AppCompat at all - neither setting the properties nor using a custom view.

When not using AppCompat both works as expected. The full source code is available here so just run the app if you're curious :)

using System.ComponentModel;
using Android.App;
using Android.Widget;
using App1.Droid.Renderers;
using Xamarin.Forms;
using Xamarin.Forms.Platform.Android;
#if __APPCOMPAT__
using NavigationRenderer = Xamarin.Forms.Platform.Android.AppCompat.NavigationPageRenderer;
#else
using NavigationRenderer = Xamarin.Forms.Platform.Android.NavigationRenderer;
#endif

[assembly: ExportRenderer(typeof(NavigationPage), typeof(NavigationPageRenderer))]

namespace App1.Droid.Renderers
{
    public class NavigationPageRenderer : NavigationRenderer
    {
        protected override void OnElementChanged(ElementChangedEventArgs<NavigationPage> e)
        {
            base.OnElementChanged(e);
            SetCustomView(e.NewElement.CurrentPage.GetType().Name);
        }

        private void SetCustomView(string view)
        {
            var activity = (Activity)Context;
#if __APPCOMPAT__
            var actionBar = ((FormsAppCompatActivity)Context).SupportActionBar;
#else
            var actionBar = activity.ActionBar;
#endif
            actionBar.Title = view;
            actionBar.Subtitle = " -> " + view;
            var abv = new LinearLayout(activity)
            {
                Orientation = Orientation.Vertical
            };

            var main = new TextView(activity)
            {
                Text = view,
            };
            main.SetTextColor(Color.Aqua.ToAndroid());
            main.SetPadding(4, 4, 2, 6);
            abv.AddView(main);
            abv.AddView(new TextView(activity)
            {
                Text = " -> " + view
            });
            actionBar.SetDisplayShowCustomEnabled(true);
            actionBar.CustomView = abv;
        }

        protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            base.OnElementPropertyChanged(sender, e);
            if (e.PropertyName.Equals("CurrentPage"))
            {
                SetCustomView(((NavigationPage)sender).CurrentPage.GetType().Name);
            }
        }
    }
}

Edit: Thanks @jimmgarr. Altered the code slightly to keep alternating between AppCompbat and "normal mode". The code is available here

like image 561
joacar Avatar asked Jul 21 '16 08:07

joacar


1 Answers

So it looks like the NavigationPage uses its own Toolbar instance. That's why setting the properties on SupportActionBar isn't doing anything.

I was able to get it working by overriding OnViewAdded() to get a reference to the new Toolbar when it's added:

public override void OnViewAdded(Android.Views.View child)
{
    base.OnViewAdded(child);

    if (child.GetType() == typeof(Support.Toolbar))
        toolbar = (Support.Toolbar)child; 
}

Then using the reference inside SetCustomView() to set only the Subtitle since the Title is already set automatically.

Here's the complete renderer class :)

like image 90
jimmgarr Avatar answered Dec 14 '22 22:12

jimmgarr