0

I have a topmost grid

<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="120"/>
<RowDefinition Height="1"/>
<RowDefinition Height="5"/>
<RowDefinition Height="35"/>
<RowDefinition Height="5"/>
</Grid.RowDefinitions>

in my page. How could I have * row to adjust it's height to respect the presence or absence of on screen keyboard? So that the content in the row 0 shrinks as the keyboard appears. Or, at the very least how could I detect keyboard shows up on an Editor? I have a custom renderer for that Editor already so stuffing extra platform specific code can be done swiftly.

Anton Tropashko
  • 5,486
  • 5
  • 41
  • 66

2 Answers2

0

You can control Grid row size only manually. Editor.Focused and Editor.Unfocused is what you're looking for.

But you can combine it with event Triggers (http://developer.xamarin.com/guides/cross-platform/xamarin-forms/working-with/triggers/):

<EventTrigger Event="Focused">
    <local:FocusedTriggerAction />
</EventTrigger>
public class FocusedTriggerAction : TriggerAction<Editor>
{
    protected override void Invoke (Editor editor)
    {
        yourRow.Height = new GridLength(100);
    }
}
Daniel Luberda
  • 7,374
  • 1
  • 32
  • 40
  • you have the fixed grid height of 100. I guess it warrants the next logical question: how could I extract the keyboard height out of the Focused event. Do you think I need to file a separate but related stackoverflow question for that? – Anton Tropashko Aug 07 '15 at 07:38
  • You'll probably need dependency injection for that (it is platform specific code). Eg. http://stackoverflow.com/questions/16788959/is-there-any-way-in-android-to-get-the-height-of-virtual-keyboard-of-device – Daniel Luberda Aug 19 '15 at 15:20
0

For now I've adapted the solution found here http://www.gooorack.com/2013/08/28/xamarin-moving-the-view-on-keyboard-show/

    private UIView activeview;             // Controller that activated the keyboard

    /// <summary>
    /// Initializes a new instance of the <see cref="ExtendedEditorRenderer"/> class.
    /// </summary>
    public ExtendedEditorRenderer ()
    {
        NSNotificationCenter.DefaultCenter.AddObserver(UIKeyboard.DidShowNotification,KeyBoardUpNotification);
        NSNotificationCenter.DefaultCenter.AddObserver(UIKeyboard.WillHideNotification,KeyBoardDownNotification);
    }

    public void Dispose()
    {
        NSNotificationCenter.DefaultCenter.RemoveObserver(UIKeyboard.DidShowNotification);
        NSNotificationCenter.DefaultCenter.RemoveObserver(UIKeyboard.WillHideNotification);
        base.Dispose();
    }
    private void KeyBoardDownNotification(NSNotification notification)
    {
        try
        {
            var view = (ExtendedEditor)Element;
            if (view.KeyboardListener != null)
            {
                Size s = new Size(0, 0);
                view.KeyboardListener.keyboardSizeChangedTo(s);
            }
        }
        catch (Exception ex)
        {
            //Debug.WriteLine("dcaught {0}", ex);
        }
    }

    private void KeyBoardUpNotification(NSNotification notification)
    {
        try
        {
            // get the keyboard size
            CGRect r = UIKeyboard.BoundsFromNotification(notification);
            Size s = new Size(r.Size.Width, r.Size.Height);
            var v = (ExtendedEditor)Element;
            v.KeyboardListener.keyboardSizeChangedTo(s);
        }
        catch(Exception ex) {
            //Debug.WriteLine("scaught {0}", ex);
        }
    }

shared platform "independent" code:

public interface IKeyboardListener
{
    void keyboardSizeChangedTo(Size s);
}

public class ExtendedEditor : Editor ...
{
Anton Tropashko
  • 5,486
  • 5
  • 41
  • 66