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

How to prevent Editor to go behind the keyboard in Xamarin.Forms?

$
0
0

Hi,

I have a Chat app. Currently, there is an 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.

To do that I have tried to replace Entry with Editor and implement following functionality.

1- Put an Editor in place of Entry.
2- Implement a functionality that keep keyboard open until user click on Message list screen or back button.

Now I am trying to implement auto height functioanlity but when user try to type, Editor goes behind the keyboard. Can anybody please suggest me how to keep Editor open and auto size?

Current code:

XAML:

Editor

public class ChatEditorWithPlaceholder : Editor
{
    public ChatEditorWithPlaceholder()
    {
        this.TextChanged += (sender, e) => { this.InvalidateMeasure(); };
    }
}

Editor Renderer:

public class ChatEditorRenderer : EditorRenderer
{
    protected override void OnElementPropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
    {
        base.OnElementPropertyChanged(sender, e);
    }
}

protected override void OnElementChanged(ElementChangedEventArgs<Editor> e)
{
    base.OnElementChanged(e);
    if(Control != null) {
        Control.ScrollEnabled = false;
    }
    var element = this.Element as ChatEditorWithPlaceholder;

    Control.InputAccessoryView = null;
    Control.ShouldEndEditing += DisableHidingKeyboard;

    MessagingCenter.Subscribe<ConversationPage>(this, "FocusKeyboardStatus", (sender) =>
    {

        if (Control != null)
        {
            Control.ShouldEndEditing += EnableHidingKeyboard;
        }

        MessagingCenter.Unsubscribe<ConversationPage>(this, "FocusKeyboardStatus");
    });
}
private bool DisableHidingKeyboard(UITextView textView)
{
    return false;
}

private bool EnableHidingKeyboard(UITextView textView)
{
    return true;
}

Screenshots:


Viewing all articles
Browse latest Browse all 89864

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>