Search code examples
androidxamarinrenderertabbedpage

Xamarin Forms android tabpage custom renderer different colors


I am trying to create a CustomRenderer for Android TabbedPage with a different color for the last tab.

However, the following code fails to work. How can I fix this?

protected override void OnVisibilityChanged(Android.Views.View changedView, [GeneratedEnum] ViewStates visibility)
        {
            base.OnVisibilityChanged(changedView, visibility);

            if(visibility == ViewStates.Visible)
            {
                var tabs = changedView.FindViewById<TabLayout>(Resource.Id.sliding_tabs);

                ViewGroup vg = (ViewGroup)tabs.GetChildAt(0);
                ViewGroup vgTab = (ViewGroup)vg.GetChildAt(vg.ChildCount - 1);

                for (int i = 0; i < vgTab.ChildCount; i++)
                {
                    Android.Views.View tabViewChild = vgTab.GetChildAt(i);

                    if(tabViewChild is Android.Widget.ImageView)
                    {
                        (tabViewChild as Android.Widget.ImageView).SetBackgroundColor(Android.Graphics.Color.Red);
                    }
                }
            }
        }

Solution

  • So, solution was very simple

    ViewGroup vg = (ViewGroup)_tabs.GetChildAt(0);
    ViewGroup vgTab = (ViewGroup)vg.GetChildAt(vg.ChildCount - 1);
    
    vgTab.SetBackgroundColor(Android.Graphics.Color.ParseColor("#E91E63"));