user2618875
user2618875

Reputation: 890

Xamarin.Forms Android custom ContentPage PageRenderer not rendering views

This doc explains how to create custom PageRenderer for Android, iOS etc. I tried as per docs but don't know why it's not working for Android. However it does works for iOS.

Shared ContentPage class:

public class SecondPage : ContentPage
    {
        public SecondPage()
        {
            Content = new StackLayout
            {
                Children = {
                    new Label { Text = "Hello ContentPage" }
                }
            };
        }
    }

Custom PageRenderer class for Android:

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

            if (e.OldElement != null || Element == null)
            {
                return;
            }

            InitView();
        }

        void InitView()
        {
            var context = Forms.Context;
            string[] os = { "Android", "iOS", "Windows" };
            var ll = new LinearLayout(context);
            ll.LayoutParameters = new LayoutParams(LayoutParams.MatchParent, LayoutParams.MatchParent);
            ll.SetBackgroundColor(Android.Graphics.Color.IndianRed);

            var rg = new RadioGroup(context);
            for (int index = 0; index < os.Length; index++)
            {
                rg.AddView(new RadioButton(context) { Text = os[index] });
            }

            ll.AddView(rg);

            AddView(ll);
        }
    }
}

Can you please tell me what went wrong ?

Upvotes: 1

Views: 2621

Answers (1)

Florian
Florian

Reputation: 485

I had similar problems. It seems the layout is incorrect. Try to set it in PageRenderer.OnLayout:

[assembly: ExportRenderer(typeof(SecondPage), typeof(SecondPageRenderer))]
namespace RenderFormsTest.Droid
{
    protected Android.Views.View NativeView;

    public class SecondPageRenderer : PageRenderer
    {
        protected override void OnElementChanged(ElementChangedEventArgs<Page> e)
        {
            base.OnElementChanged(e);

            if (e.OldElement != null || Element == null)
            {
                if (Element == null)
                    NativeView = null;

                return;
            }

            InitView();
        }

        void InitView()
        {
            var context = Forms.Context;
            string[] os = { "Android", "iOS", "Windows" };
            var ll = new LinearLayout(context);
            ll.LayoutParameters = new LayoutParams(LayoutParams.MatchParent, LayoutParams.MatchParent);
            ll.SetBackgroundColor(Android.Graphics.Color.IndianRed);

            var rg = new RadioGroup(context);
            for (int index = 0; index < os.Length; index++)
            {
                rg.AddView(new RadioButton(context) { Text = os[index] });    
            }

            ll.AddView(rg);

            AddView(ll);

            NativeView = ll;
        }

        protected override void OnLayout(bool changed, int l, int t, int r, int b)
        {
            base.OnLayout(changed, l, t, r, b);

            if ( NativeView != null )
            { 
                var msw = MeasureSpec.MakeMeasureSpec(r - l, MeasureSpecMode.Exactly);
                var msh = MeasureSpec.MakeMeasureSpec(b - t, MeasureSpecMode.Exactly);

                NativeView.Measure(msw, msh);
                NativeView.Layout(0, 0, r - l, b - t);
            }
        }
    }
}

Upvotes: 2

Related Questions