[Material] how to gain access to `Toolbar` from custom renderer?

ChaseFlorellChaseFlorell CAInsider, University mod

Based on @TheRealJasonSmith's gist on how to add Material design to your Forms.Android app, I've implemented the "Toolbar" instead of the "ActionBar".

I'm wondering how I might go about getting access to that View in order to set the NavigationIcon to the Page.Icon?

Right now I've got a custom renderer that is attempting to set the Toolbar NavigationIcon like so, but toolbar is always null

[assembly: ExportRenderer(typeof(Page), typeof(Renderers.ExtendedPageRenderer))]
namespace Renderers
{
    public class ExtendedPageRenderer : PageRenderer
    {
        protected override void OnElementChanged(ElementChangedEventArgs<Page> e)
        {
            base.OnElementChanged(e);

            if (Element != null && Element.Icon != null)
            {
                var iconId = ResourceManager.GetDrawableByName(Element.Icon.File);
                var toolbar = FindViewById<Toolbar>(Resource.Id.toolbar);
                if (toolbar != null)
                {
                    toolbar.SetNavigationIcon(iconId);
                }
            }
        }
    }
}

Best Answer

Answers

  • ChaseFlorellChaseFlorell CAInsider, University mod

    Just noticed that when I rotate the device, this falls apart, still digging.

  • JamesMontemagnoJamesMontemagno USForum Administrator, Xamarin Team, Developer Group Leader Xamurai

    It should be the SupportActionBar I believe?

  • DanielLDanielL PLInsider ✭✭✭✭
    edited October 2015

    @ChaseFlorell I don't know if it would be useful, but I had a similar problem (rotation change etc) while trying to set custom font in actionbar. The only working solution was to use a custom LayoutInflater. Look here: https://github.com/daniel-luberda/XamarinFormsToolbarCustomFont/blob/master/Droid/CustomLayoutInflaterFactory.cs

  • ChaseFlorellChaseFlorell CAInsider, University mod

    @JamesMontemagno, what library does that live in? I can't seem to find it in the Android.Support.V7.Widget namespace. Right now I'm using Android.Support.V7.Widget.Toolbar.

    @DanielL thanks for that. I want to try a little more at getting this working "as is" before jumping to full custom renderers. We'll see how it fairs.

  • ChaseFlorellChaseFlorell CAInsider, University mod
    edited October 2015

    @JamesMontemagno, I found it (assumed it was a class, not a property).

    I've exposed it, in my main activity

    public MainActivity()
    {
        _this = this;
    }
    
    public static ActionBar RootActionBar{ get { return _this.SupportActionBar; } }
    

    and then tried to access it.

    var iconId = ResourceManager.GetDrawableByName(Element.Icon.File);
    MainActivity.RootActionBar.SetLogo(iconId);
    

    but the result is that there is no icon at all (My old approach was better). However, I still have the rotation issue that I'm working through.

  • DanielLDanielL PLInsider ✭✭✭✭
    edited October 2015

    @ChaseFlorell Ok, good luck. Just a little explanation: If you use custom layout inflater just handle the view you're interested to change (just set the icon). Return null for all others (to get the default) and you're done.

  • sgousgou DEMember ✭✭

    @DanielLuberda i left a request on github: https://github.com/daniel-luberda/XamarinFormsToolbarCustomFont/issues/1
    for a change so that your library work with Android support Library as well. Do you have a suggestion? I would appreciate your help.

  • DanielLDanielL PLInsider ✭✭✭✭
  • DavidConliskDavidConlisk GBMember ✭✭

    I used @ChaseFlorell solution but to avoid the need to add the public static member to the MainActivity, you can use this code in your custom renderer to get a reference to the toolbar. This means all the code required resides in your custom renderer.

    var context = (Activity) Xamarin.Forms.Forms.Context;
    var toolbar = context.FindViewById<Toolbar>(Droid.Resource.Id.toolbar);
    
  • AbhinashPatiAbhinashPati USMember
    edited August 2017

    I'm having the Rotation issue. Has anyone solved it?
    What i'm trying to do is Override The Back button in toolbar.
    I used this approach for doing this
    Override Navigation

    Its working fine unless the device orientation changes. Once the orientation changes, this override doesn't fire up on the current page or any other page that has this approach.
    I even tried Creating a custom Button in the toolbar and Explicitly Hiding and UnHiding it on the screen required using Dependency Service and the above mentioned method, but here also the Button resets to it original state as in the layout file.

Sign In or Register to comment.