3

I have a TextField in a container (VBox) in the bottom. When i select the TextField to enter some text it gets hidden behind the keyboard (iPhone). I put the VBox in ScrollPane but still the same.

Can i get the keyboard somehow to get its height? How do i place TextFields which are not covered from keyboard?

Thank you for your help.

tonimaroni
  • 1,062
  • 10
  • 19

1 Answers1

4

At this moment, there is no built-in method in JavaFX or JavaFXPorts to get the (native) iOS soft keyboard.

The solution to get the keyboard and find out if any node, like a TextField will be covered by it, would require a Service from those available in the Gluon Charm Down library, but for now there is no such KeyboardService.

Based on native solutions like this, it's easy to get a notification when the keyboard is being shown or hidden. So we could make use of those listeners and send the height value back to the JavaFX layer.

So let's create the KeyboardService taking into account how the services are created in the Charm Down library.

Since this is a little bit out of scope here, I've created this gist with the required files.

Follow these steps to make it work:

  1. Create a Gluon Project

Create a Gluon project (single view) with the latest version of the Gluon plugin for your IDE.

  1. Add the KeyboardService interface

Add the package com.gluonhq.charm.down.plugins. Add the classes KeyboardService (link) and KeyboardServiceFactory (link).

public interface KeyboardService {
    public ReadOnlyFloatProperty visibleHeightProperty();
}
  1. iOS implementation

Under the iOS package add the iOS implementation of the service IOSKeyboardService (link).

public class IOSKeyboardService implements KeyboardService {

    static {
        System.loadLibrary("Keyboard");
        initKeyboard();
    }

    private static ReadOnlyFloatWrapper height = new ReadOnlyFloatWrapper();

    @Override
    public ReadOnlyFloatProperty visibleHeightProperty() {
        return height.getReadOnlyProperty();
    }

    // native
    private static native void initKeyboard();

    private void notifyKeyboard(float height) {
        Platform.runLater(() -> this.height.setValue(height));
    }

}
  1. Native code

Create a native folder under /src/ios and add the Keyboard.h (link) file:

#import <UIKit/UIKit.h>
#include "jni.h"

@interface Keyboard : UIViewController {}
@end

void sendKeyboard();

and the Keyboard.m (link) file:

static int KeyboardInited = 0;
jclass mat_jKeyboardServiceClass;
jmethodID mat_jKeyboardService_notifyKeyboard = 0;
Keyboard *_keyboard;
CGFloat currentKeyboardHeight = 0.0f;

JNIEXPORT void JNICALL Java_com_gluonhq_charm_down_plugins_ios_IOSKeyboardService_initKeyboard
(JNIEnv *env, jclass jClass)
{
    if (KeyboardInited)
    {
        return;
    }
    KeyboardInited = 1;

    mat_jKeyboardServiceClass = (*env)->NewGlobalRef(env, (*env)->FindClass(env, "com/gluonhq/charm/down/plugins/ios/IOSKeyboardService"));
    mat_jKeyboardService_notifyKeyboard = (*env)->GetMethodID(env, mat_jKeyboardServiceClass, "notifyKeyboard", "(F)V");
    GLASS_CHECK_EXCEPTION(env);

    _keyboard = [[Keyboard alloc] init];
}

void sendKeyboard() {
    GET_MAIN_JENV;
    (*env)->CallVoidMethod(env, mat_jKeyboardServiceClass, mat_jKeyboardService_notifyKeyboard, currentKeyboardHeight);
}

@implementation Keyboard 

- (void) startObserver 
{
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
}

- (void) stopObserver 
{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
}

- (void)keyboardWillShow:(NSNotification*)notification {
    NSDictionary *info = [notification userInfo];
    CGSize kbSize = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size;
    currentKeyboardHeight = kbSize.height;
    sendKeyboard();
}

- (void)keyboardWillHide:(NSNotification*)notification {
    currentKeyboardHeight = 0.0f;
    sendKeyboard();
}

@end

  1. Build the native library

On a Mac with a recent version of XCode, you can build the native library libKeyboard.a. For that you need to add the xcodebuild task to the build.gradle file of the project (link). It is based on the ios-build.gradle file from Charm Down.

task xcodebuild {
    doLast {
        xcodebuildIOS("$project.buildDir","$project.projectDir", "Keyboard")
    }
}

Save your project, and run ./gradlew clean build xcodebuild from command line under the project root.

If everything is in place you should find libKeyboard.a under build/native. Copy the file, create the folder jniLibs under src/ios, and paste it there.

  1. Implement the service

Add a TextField to the BasicView, and change the alignment to BOTTOM-CENTER.

VBox controls = new VBox(15.0, label, button, new TextField());
controls.setAlignment(Pos.BOTTOM_CENTER);

Implement the service:

Services.get(KeyboardService.class).ifPresent(keyboard -> {
    keyboard.visibleHeightProperty().addListener((obs, ov, nv) -> 
        setTranslateY(-nv.doubleValue()));
});
  1. Deploy and run

You should have everything in place. Plug your iPhone/iPad, and run ./gradlew --info launchIOSDevice.

When the textField gets the focus, the soft keyboard shows up, and the view is translated, so the textField is fully visible:

Hopefully this service will be included in Charm Down at some point. But this is also a good example of how you can add custom services. Also note the Charm Down project is open source, so any contribution is wellcome.

Community
  • 1
  • 1
José Pereda
  • 44,311
  • 7
  • 104
  • 132
  • Brilliant! Thank you!! – tonimaroni Nov 28 '16 at 19:25
  • Hey José. I only found time today to implement the above plugin. Perfectly described and all is working. I have a "Send" Button right to a textfield. Whit the solution above i have to press "Send" twice. The firsttime it removes the keyboard.. then i have to click the button again to get it doing his job. Do you have an idea what i need to change so when keyboard is active pressing a button will be triggered the first time. Thank you. – tonimaroni May 03 '17 at 11:45
  • Replace `button.setOnAction()` with `button.setOnMouseClicked()`, that should work. Somehow the `fire()` event is consumed when the keyboard is dismissed and `onAction` is not triggered. – José Pereda May 03 '17 at 18:03
  • That did not work either. But it gave me the right hint anyway. setOnTouchPressed is triggered properly. Thank you again! – tonimaroni May 04 '17 at 08:42
  • Hi José. Deploying to iOs was fine. Now i have set up Android but fail to deploy it there. Do i need to uncomment all the above and remove the lib from the src folder? I am struggeling now with the error INSTALL_FAILED_NO_MATCHING_ABIS which seems to tell that i have the wrong architecture.. Any idea? – tonimaroni Jun 06 '17 at 19:19
  • Are you deploying to an Android emulator? That won't work. Anyway it's better if you post a new question. – José Pereda Jun 06 '17 at 19:25
  • Yes, i was deploing on android emulator. You are right, i will create a new question. – tonimaroni Jun 06 '17 at 19:27