2

I wish to write a dependency service to get the keyboard height when the keyboard appears after tapping any entry cell.

Interface in PCL

public interface IKeyboardService
    {
        int GetHeight();
    }

Xaml code

 <ContentPage.Content> 
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="*" />
                <RowDefinition Height="auto" />
            </Grid.RowDefinitions>

            <StackLayout Grid.Row="1">
                <Entry Text="Tap to open the keyboard" Focused="entryUserName_Focused" />
                <Button Text="Submit" Clicked="Handle_Clicked" />
            </StackLayout>
        </Grid>     
    </ContentPage.Content>

Behind code

   private void entryUserName_Focused(object sender, FocusEventArgs e)
        {
            int height= DependencyService.Get<IKeyboardService>().GetHeight();
        }

Overriding interface in Android

   public class Keyboard_Droid : IKeyboardService
    {
      public int GetHeight()
        {
            // Logic to implement
            return h;
        }
    }

Overriding interface in IOS

public class Keyboard_IOS : IKeyboardService
        {
          public int GetHeight()
            {
                // Logic to implement
                return h;
            }
        }

Can anyone suggest the implementation to achieve this.

Himanshu Dwivedi
  • 7,934
  • 3
  • 31
  • 52
  • 1
    Look at : http://stackoverflow.com/questions/13534365/getting-the-dimensions-of-the-soft-keyboard and http://arsenkin.com/how-to-get-ios-keyboard-height-programmatically.html – Rohit Vipin Mathews Sep 23 '16 at 14:05
  • 1
    What about rootView in android and NSNotificationCenter in IOS. How should I pass these values from PCL. – Himanshu Dwivedi Sep 23 '16 at 14:09

1 Answers1

-1

Xamarin Forms Code Behind file If you check the height of the MainPage, immediately after focused is triggered, then it will return the device height. However, I added a slight delay and was able to see the height of the screen after the onscreen keyboard is shown.

private async void entryUserName_Focused(object sender, FocusEventArgs e)
{
    double fullHeight = Application.Current.MainPage.Height; 
    //Note: adjust 350ms if height results are inconsistent.
    await Task.Delay(350); 
    double availableScreenHeight = fullHeight - Application.Current.MainPage.Height;
}
chri3g91
  • 1,196
  • 14
  • 16