Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 58056

Custom renderer dynamic height

$
0
0

I have a small custom ViewRenderer that has a linear layout with a couple of Text Views inside. I would like to show/hide one of the Text Views and have everything resize to fill the empty space when that Text View is hidden. A simple example would be like this..

image
image

So, of course, that is just two box views with my custom control between them. I'd like the height of my control to change when I show and hide the "Some More Text" text view.

The code for the renderer is below. Is there anything I can do in OnElementPropertyChanged to force the height of my control to change after I set the text view's visibility?

Thanks!

protected override void OnElementChanged(ElementChangedEventArgs e)
{
base.OnElementChanged(e);

        if (Control == null)
        {
            LinearLayout mainLayout = new LinearLayout(Context);
            mainLayout.LayoutParameters = new LinearLayout.LayoutParams(LayoutParams.MatchParent, LayoutParams.WrapContent); 
            mainLayout.Orientation = Orientation.Vertical;

            TextView tvLabel = new TextView(Context);
            tvLabel.Text = "Some Text";

            TextView tvText = new TextView(Context);
            tvText.Id = 1;
            tvText.Text = "Some More Text";

            mainLayout.AddView(tvLabel);
            mainLayout.AddView(tvText);
            SetNativeControl(mainLayout);
        }
    }
    protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
    {
        TextView tvText;

        base.OnElementPropertyChanged(sender, e);
        if (Control == null || Element == null) return;
        if (e.PropertyName == "ShowTextField")
        {
            MyControl em = (MyControl)Element;
            tvText = Control.FindViewById<TextView>(1);
            tvText.Visibility = (em.ShowTextField) ? Android.Views.ViewStates.Visible : Android.Views.ViewStates.Gone;                    
        }
    }

Viewing all articles
Browse latest Browse all 58056

Trending Articles