How to make entry like editor in Xamarin.Forms?

developer007developer007 Member ✭✭
edited May 16 in Xamarin.Forms

Hi,

I am creating a mobile application in that I need to send a message for query or support when the user creates that type of message and then click on return or done from keyboard entry height should be adjusted and if the user clicks on the back than it should also be managed. In that, I am not able to use Editor control. I am using a shared project to develop the application.

I try with the following code:

Sample.xaml

 <StackLayout Padding="50" HeightRequest="150">
    <Entry TextChanged="Entry_TextChanged" x:Name="EntryCustom"/>
 </StackLayout>

Sample.xaml.cs

private void Entry_TextChanged(object sender, TextChangedEventArgs args)
{
    int isElse = 0;
    if (args != null && args.OldTextValue != null && args.NewTextValue != null)
    {
        string[] oldval = args.OldTextValue.Split('\r');
        string[] Newval = args.NewTextValue.Split('\r');
        if (oldval.Count() > Newval.Count())
        {
            EntryCustom.HeightRequest = EntryCustom.HeightRequest - 33;
            isElse = 2;
        }
        else
            isElse = 1;
    }
    if (args.OldTextValue != null && (args.NewTextValue == "\n" || args.NewTextValue.EndsWith("\r")) && EntryCustom.HeightRequest < 100 && (isElse == 1 || args != null || args.OldTextValue.Count() > args.NewTextValue.Count()))
        EntryCustom.HeightRequest = EntryCustom.HeightRequest + 33;
}

Can anyone look into this and suggest me what should I have to change in the code?

Answers

  • DirkWilhelmDirkWilhelm USMember ✭✭✭✭

    Why can't you use the editor with the autosize property set to "TextChanges"?

    <Editor AutoSize="TextChanges" Text="{Binding Whatever}"/>

  • developer007developer007 Member ✭✭

    @DirkWilhelm said:
    Why can't you use the editor with the autosize property set to "TextChanges"?

    <Editor AutoSize="TextChanges" Text="{Binding Whatever}"/>

    If I am using editor than can you please know how to set MaximumHeightRequest and also in keyboard how to hide done button that is at top of the keyboard that I display in screenshot.

  • JohnHardmanJohnHardman GBUniversity mod

    @developer007 said:
    If I am using editor than can you please know how to set MaximumHeightRequest and also in keyboard how to hide done button that is at top of the keyboard that I display in screenshot.

    Just curious - why would you not want a Done button?

  • developer007developer007 Member ✭✭

    Hi John,

    Thanks for your question. Actually, I have a Chat app. Currently, there is a Entry control for add chat text. Now I want to provide multiline Entry, same like Whatsapp.

    • If user type more than one line, it should automatic wrap the text to next line.
    • If user click on Nextline button in mobile keyboard, it should go to next line.
    • Height of Entry should be automatically increase upto 3 line and it should also decrease if user remove text.

    If I use Editor in place of Entry, user need 2 click(Tap) to send a message. 1- "Done" 2- "Send" button

    Please suggest your thoughts!

  • FaizalSaidaliFaizalSaidali USMember ✭✭

    Hi @developer007,
    I think you can handle it by the Editor. You can remove the done button by using rendering the Editor and set null on InputAccessoryView property.
    Then the height issue will resolve with AutoSize, then override the Editor's property changed event, and handle it with the height limit.

  • JoeMankeJoeManke USMember ✭✭✭✭✭
    edited May 17

    Either way you will need to use a custom renderer, whether it's to allow the Entry to be multi-line or change the InputAccesoryView on the Editor.

  • JohnHardmanJohnHardman GBUniversity mod

    I would start with the Editor, rather than make an Entry multi-line. The key thing about Entry is that it's single-line, changing that will confuse anybody maintaining the code.

  • AdamMeaneyAdamMeaney USMember ✭✭✭✭✭

    MaxHeight is pretty easy to implement you own BindableProperty for in a subclassed Editor.

            public static readonly BindableProperty MaxHeightProperty = BindableProperty.Create(
                nameof(MaxHeight),
                typeof(double),
                typeof(CustomEditor),
                -1d);
    
            public double MaxHeight
            {
                get => (double)GetValue(MaxHeightProperty);
                set => SetValue(MaxHeightProperty, value); 
            }
    
            protected override SizeRequest OnMeasure(double widthConstraint, double heightConstraint)
            {
                if (MaxHeight > 0)
                {
                    heightConstraint = Math.Min(MaxHeight, heightConstraint);
                }
    
                if (MaxWidth > 0)
                {
                    widthConstraint = Math.Min(MaxWidth, widthConstraint);
                }
    
                var measurement = base.OnMeasure(widthConstraint, heightConstraint);
                return measurement;
         }
    
Sign In or Register to comment.