Show soft input programmatically in android


#1

I’m trying to set focus and displaying the keyboard programmatically but the code below only works for iOS:

const textField = <TextField>this.addTextField.nativeElement;
textField.focus();

What Im trying in Android is the code below based on this thread but I have an Angular 2 project instead https://github.com/NativeScript/NativeScript/issues/1299:

showSoftInput() {
    const textField = <TextField>this.addTextField.nativeElement;
    const imm = utils.ad.getInputMethodManager();
    imm.showSoftInput(textField, textField.android.view.inputmethod.InputMethodManager.SHOW_IMPLICIT);
  }

But I get an exception. Anyone know how to achieve this?


#2

What’s the exception?

I’ve written how to hide the keyboard which might help in the code where you need to get the input manager. https://bradmartin.net/2016/10/21/dismiss-the-android-softkeyboard-programmatically-in-a-nativescript-app/

Just scanning your code it doesn’t look right. On my phone or I’d show you another way. When you provide the exception it’ll be easy to figure out what’s happening though.


#3

I’ll paste the stack trace when Im back to my other computer. Regarding the link, where do you get the reference to “android” in the line below?

let inputManager = Context.getSystemService(android.content.Context.INPUT_METHOD_SERVICE);

#4

Just accessing the native android namespace. If you want intellisense and
use typescript the NS team has a typings for some help there. think it’s
tns-platform-declarations on npm.


#5

The code snippet from my first post actually works, I just forgot to reference the android property of TextField.

showSoftInput() {
    const textField = <TextField>this.addTextField.nativeElement;
    const imm = utils.ad.getInputMethodManager();
    imm.showSoftInput(textField.android, textField.android.view.inputmethod.InputMethodManager.SHOW_IMPLICIT);
  }

The only thing missing now is that my TextField doesn’t get focus, so when I start typing nothing happens.