1
votes

I have been struggling with this for a few days now, i have tried lots of the articles on SO and none seem to match my requirements, i have set the Toolbar.xml and set the theme as below

<style name="ToolbarStyle" parent="Theme.AppCompat.Light">
    <!-- Customize color of navigation drawer icon and back arrow -->
    <item name="colorControlNormal">@color/white</item>
  </style>

This works on a per app basis, but i need to alternate between white and black arrows depending on the content page i am on, i have tried custom renderers as well but this doesn't work dynamically as well

Any pointers massively appreciated, even if i can clarify, is the Xamarin forms navigation a Toolbar or an ActionBar?

Cheers Anthony

1
Use different-2 custom theme for different content page.Vijendra patidar
Hi, i thought about this, but i wasn't sure how to change the theme dynamically on the Android toolbar per xamarin content page?Anthony Kallay

1 Answers

0
votes

is the Xamarin forms navigation a Toolbar or an ActionBar?

It is a Toolbar and not an ActionBar

Android.Support.V7.Widget.Toolbar

You can set the color via the ToolBar.NavigationIcon.SetColorFilter(...), but you have to find it first as Xamarin is not applying it via SetSupportActionBar...

Red backbutton PlatformEffect example:

public class RedBackButtonColorNameEffect : PlatformEffect
{
    protected override void OnAttached()
    {
        void ViewGroups(ViewGroup viewGroup)
        {
            for (int i = 0; i < viewGroup.ChildCount; i++)
            {
                Android.Views.View v = viewGroup.GetChildAt(i);
                switch (v)
                {
                    case Toolbar tb:
                        tb.NavigationIcon?.SetColorFilter(viewGroup.Resources.GetColor(Android.Resource.Color.HoloRedDark), PorterDuff.Mode.SrcAtop);
                        return;
                    case ViewGroup vg:
                        ViewGroups(vg);
                        break;
                }
            }
        }
        var rootView = (Control.Context as AppCompatActivity).FindViewById(Android.Resource.Id.Content).RootView as ViewGroup;
        ViewGroups(rootView);
    }


    protected override void OnDetached()
    {
    }
}

Usage:

someControl.Effects.Add(Effect.Resolve("Effects.RedBackButtonColorNameEffect"));

(remember set your ResolutionGroupName & ExportEffect attributes...)

Note: Xamarin does not apply a value for the Control instance within a PlatformEffect if you attach the effect at the page level or a container level and thus this will not work. You could store the Activity context somewhere (static var) from the MainActivity in order to workaround that issue and thus have the proper Context to search within, your choice..

Note!: PlatformEffects are whack in so many ways, especially since they never get garage collected and thus are a memory leak, they are nice for a quick example/prototype, but use a custom renderer for production code).