Forum Xamarin.Forms

Xamarin Forms Custom View Renderer for TextView Height issue

I want to render xamarin forms view control into android textview.

Below customrenderer renders Android TextView on Android device with initial default text.

Now, If I add more text into it, this control would not grow in height but instead becomes scrollable to see the text not visible on screen.

On other way around, if I reduce text content, this control would not reduce in height and occupy the same space and show reduced text content on scree.

Issue : In this case of text change, this label should increase or reduce height on screen for Android. It is working fine on IOS as expected.

Here is a sample code.

class CopyableLabelRenderer : ViewRenderer
    {
        protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.View> e)
        {
            base.OnElementChanged(e);
            try
            {
                var label = new TextView(MainApplication.Context);
                label.SetTextIsSelectable(true);
                label.LinksClickable = true;

                label.Text = (e.NewElement as CopyableLabel).BindingText; // this line sets text into this label

                SetNativeControl(label);
            }
            catch (Exception ex)
            {
                var msg = ex.Message;
            }
        }

        protected override void OnElementPropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            base.OnElementPropertyChanged(sender, e);

            if (e.PropertyName.Equals("BindingText"))
            {
                TextView textView = this.Control as TextView;
                textView.Text = (Element as CopyableLabel).BindingText;

            }
        }

    }

Here is a base class,

public class CustomLabel : View
    {
        public static readonly BindableProperty BindingTextProperty = BindableProperty.Create("BindingText", typeof(string),typeof(View),
                string.Empty
               );

        private int _fontSize;
        private bool _request = false;

        public string BindingText
        {
            get
            {
                return (string)GetValue(BindingTextProperty); 
            }

            set
            {
                SetValue(BindingTextProperty, value);
            }
        }

    }
Sign In or Register to comment.