Michael Sabin
Michael Sabin

Reputation: 1759

Windows Phone Get Keyboard Height

I am working on a Windows Phone 8 application that i need to do some custom handling when the keyboard is displayed. Is there any way that I can get height of the keyboard in the current orientation.

Thanks

Upvotes: 4

Views: 2206

Answers (4)

Vinícius Rocha
Vinícius Rocha

Reputation: 11

For Windows Phone 8.1 RT, just add the following code in your xaml.cs file.

// This part in the constructor of the page
InputPane inputPane = InputPane.GetForCurrentView();
inputPane.Showing += InputPane_Showing;
inputPane.Hiding += InputPane_Hiding;

private void InputPane_Hiding(InputPane sender, InputPaneVisibilityEventArgs args) {
     // Do what you want.
}

private void InputPane_Showing(InputPane sender, InputPaneVisibilityEventArgs args) {
    Debug.WriteLine("Keyboard Height: "sender.OccludedRect.Height);
}

Upvotes: 0

Nigel Leeming
Nigel Leeming

Reputation: 21

I managed it with the following in Windows Phone 8.1, but there wasn't much room left for the textbox with a save button beneath and the pivot headers above.

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="*" />
        <RowDefinition Height="Auto" />
        <RowDefinition Height="Auto" />
    </Grid.RowDefinitions>
    <!--ScrollViewer Background="#302010" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"-->
        <TextBox  x:Name="text" 
                  ManipulationDelta="textList_ManipulationDelta" ManipulationMode="Scale" 
                  HorizontalAlignment="Stretch" VerticalAlignment="Stretch" 
                  TextWrapping="Wrap" AcceptsReturn="True"
                  >
        </TextBox>
    <Rectangle x:Name="rectRed" Fill="Red" Grid.Row="2" Height="10" />
    <Rectangle HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Grid.Row="1" Fill="#30000000" />
    <StackPanel x:Name="stackAppBar" Orientation="Horizontal" Grid.Row="1" HorizontalAlignment="Center">
        <AppBarButton Icon="Save" Label="Save" IsCompact="true" x:Name="buttonSave" Click="buttonSave_Click"/>
        <!--AppBarButton Icon="Delete" Label="Delete"  IsCompact="true"/-->
    </StackPanel>
    <!--Rectangle x:Name="rectKeyboard" Fill="Red" Grid.Row="2" Height="200" /-->
</Grid>

async void UCSetup_Loaded(object sender, RoutedEventArgs e)
    {
        // put text in box
        string s = await IO.ReadSetupFile();
        text.Text = s;

        InputPane.GetForCurrentView().Showing += UCSetup_Showing;
        InputPane.GetForCurrentView().Hiding += UCSetup_Hiding;
    }

    void UCSetup_Hiding(InputPane sender, InputPaneVisibilityEventArgs args)
    {
        var size = args.OccludedRect;
        rectRed.Height = size.Height;

    }

    void UCSetup_Showing(InputPane sender, InputPaneVisibilityEventArgs args)
    {
        var size = args.OccludedRect;
        rectRed.Height = size.Height;

    }

Upvotes: 2

Deeko
Deeko

Reputation: 1539

There isn't a good way to get the actual height of the keyboard, but if the keyboard caused the screen to scroll because the text box was on the bottom half, you can find out how far it was scrolled by looking at the root frame's offset.

See How to determine the keyboard offset for more details / code.

Upvotes: 1

Oren
Oren

Reputation: 3248

There's no good way of doing this in the XAML framework, though you can simply trigger your code whenever a TextBox.GotFocus event is fired. Easiest way to do that is simply subclass TextBox and fire a custom event of your own when the parent GotFocus event is triggered.

If you're writing a pure native app then you can find this by checking the occluded region of your window. An example of this can be found right at the end of: http://msdn.microsoft.com/en-us/library/windowsphone/develop/jj247546(v=vs.105).aspx

Upvotes: 1

Related Questions