Unable to hide keyboard


Hi all,

I am trying to hide keyboard by clicking on parent StackLayout. I have tried literally all examples I could find online and nothing worked. Most examples seems to be written for core JS and not for Angular. I am using Angular with Typescript. I probably missed something. I am working on Android emulator, but I would need a solution to work on both android and iOS. Here is my code:

@ViewChild("testTextField") testTextField: ElementRef;
 var myTextfield = <TextField>this.testTextField.nativeElement;

<StackLayout height="200"  backgroundColor="lime" (tap)="clearTextFieldFocus(args)">

      <TextField #testTextField ></TextField>

Any help is highly appreciated. Thank you.


Just works as expected, can you try reproducing the issue in playground?


@manojdcoder thank you for prompt reply. I have tried in Nativescript Playground and it is working fine. The problem was that I haven’t added [(ngModel)]. Here is link to my playground, I have added nativescript-dom plugin, so it is easy to hide keyboard no matter which TextField it was open from. https://play.nativescript.org/?template=play-ng&id=dcTScU


@bachras I think the solution you follow may not be feasible, running a loop against a class name. Here is an easy native solution, without any loop without worrying which textfield is focused.

import * as utils from "utils/utils";
import { isIOS, isAndroid } from "platform";
import * as frame from "ui/frame";

function dismissSoftKeybaord(){
 if (isIOS) {
 if (isAndroid) {

Hide keyboard without referencing to textField

Your solution is even better. Thank you very much. Have a nice weekend!


@manojdcoder I have just tried your code on my app and it works fine on main page, but it doesn’t hide keyboard if I am on modal dialog. Do you have any idea how to solve this issue? Thanks


Is it on iOS / Android?


Unfortunately, it doesn’t work on both iOS and Android. Any ideas?


@bachras At least I never had to use this with modal dialogs. Try the below one, it should also work with modal dialogs.

import * as application from "application";
import { isIOS, isAndroid } from "platform";
import * as utils from "utils/utils";

declare var UIApplication, android;

export function dismissSoftKeybaord() {
    if (isIOS) {
        utils.ios.getter(UIApplication, UIApplication.sharedApplication)
    if (isAndroid) {
        const dialogFragment = application.android
        if (dialogFragment) {
        } else {

Note: The way how modal dialog (DialogFragment) is created, seems changed in the upcoming version of NativeScript, this code may need fixing then.


Thank you very much.


This worked for me, thanks! Been trying to figure this one out for ages!

I’ve created my own dropdown select element and needed the keyboard to close when the user focus’s on the dropdown after clicking a TextField.


this would make your code perfect :wink:

else if (isAndroid)