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
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 :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With