NOTE! You are browsing legacy documentation. For latest visit

NativeScript Core


To use the functionality provided by the utils/utils module, first require/import the module:

const utilsModule = require("tns-core-modules/utils/utils");
import * as utils from "tns-core-modules/utils/utils";

isFileOrResourcePath() function

Verify if the specified path points to a resource or a local file. The function returns a boolean value of:

  • true - if the path has a valid path structure
  • false - if the path is not a file path
var path = "res://icon";
var value = utilsModule.isFileOrResourcePath(path);
const path: string  = "res://icon";
const value: boolean = utils.isFileOrResourcePath(path);

isDataURI() function

Checks if the specified URI is a data URI.

  • Returns true if the string is data URL, otherwise false
var url = "<url>";
var value = utilsModule.isDataURI(url);
const url: string = "<url>"; 
const value:boolean = utils.isDataURI(url);

openUrl() function

Open an URL on device with the default browser


escapeRegexSymbols() function

Escapes special regex symbols (., *, ^, $, etc.) in string in order to create a valid regex from it.

var sampleString = "All of these should be escaped: ^ $ * ";
var newString = utilsModule.escapeRegexSymbols(sampleString);
var sampleString: string = "All of these should be escaped: ^ $ * ";
var newString: string = utils.escapeRegexSymbols(sampleString);

convertString() function

Converts a string value to a number or boolean;

var stringToBoolean = "true";
var booleanValue = utilsModule.convertString(stringToBoolean);

var stringToNumber = "23";
var numberValue = utilsModule.convertString(stringToNumber);
const stringToBoolean :string = "true";
const booleanValue :boolean = utils.convertString(stringToBoolean);

const stringToNumber: string = "23";
const numberValue :number = utils.convertString(stringToNumber);

getDisplayDensity() function

Returns the display density of the device.

var displayDensity = utilsModule.layout.getDisplayDensity();
const displayDensity = utils.layout.getDisplayDensity();

toDevicePixels() function

Converts value from device independent pixels to device pixels.

var devicePixels = utilsModule.layout.toDevicePixels(<dip>);
const devicePixels = utils.layout.toDevicePixels(<dip>);

toDeviceIndependentPixels() function

Convert value to device independent pixels.

var deviceIndependentPixels = utilsModule.layout.toDeviceIndependentPixels(<px>);
const deviceIndependentPixels = utils.layout.toDeviceIndependentPixels(<px>);

round() method

Rounds value used in layout.

var value = utilsModule.layout.round(<number_value>);
var value = utils.layout.round(<number_value>);

If we set 123.56px as a input the returned value will be 124px.

executeOnMainThread() method

The method checks if the current thread is the main thread. It will directly call the passed function if it is, or dispatches it to the main thread otherwise.

utilsModule.executeOnMainThread(() => {
    // ...
utilsModule.executeOnMainThread(() => {
    // ...

mainThreadify() method

The method returns a function wrapper which executes the supplied function on the main thread. The wrapper behaves like the original function and passes all of its arguments BUT discards its return value.

utilsModule.mainThreadify(() => {
    // ...
utilsModule.mainThreadify(() => {
    // ...

Platform specific methods


getApplication() function

Returns an instance of native Android application The returned value will be of type

var application =;
const application =;

getApplicationContext() function

Returns the Android application context. The returned value will be of type android.content.Context.

var context =;
const context =;

getInputMethodManager() function

Returns an instance of native Android input method manager. The returned value will be an android.view.inputmethod.InputMethodManager

var inputMethodManager =;
const inputMethodManager =;

showSoftInput() function

Show keyboard for a specific element.

<TextField id="textfieldid" hint="Target field" />
<Button text="Show keyboard" tap="showKeyboard" class="btn btn-primary btn-active"/>
function showKeyboard(args) {
    var button = args.object;
    var page =;
    var textField = page.getViewById("textfieldid");;
exports.showKeyboard = showKeyboard;
import { Page } from "tns-core-modules/ui/page";
import { TextField } from "tns-core-modules/ui/text-field";
import { Button } from "tns-core-modules/ui/button"

export function showKeyboard(args: EventData) {
    var button: Button = <Button>args.object;
    var page: Page = <Page>;
    var textField: TextField = <TextField> page.getViewById("textfieldid");;

dismissSoftInput() function

Hides the soft input method, usually a soft keyboard.

<TextField id="textfieldid" hint="Target field" />
<Button text="Hide keyboard" tap="dismissSoftInput" class="btn btn-primary btn-active"/>
function dismissSoftInput(args) {;
exports.dismissSoftInput = dismissSoftInput;
export function dismissSoftInput(args: EventData) {;

stringArrayToStringSet() function

Converts a string array into a String hash set.

var stringArr = ["a", "b", "c"]
var stringSet =;
const stringArr = ["a", "b", "c"]
const stringSet =;

stringSetToStringArray() function

Converts a string hash set into array of strings

var hashSet = new java.util.HashSet();
var string1 = new java.lang.String("item1");
var string2 = new java.lang.String("item2");
var string3 = new java.lang.String("item3");


var stringArray =;
const hashSet = new java.util.HashSet();
const string1 = new java.lang.String("item1");
const string2 = new java.lang.String("item2");
const string3 = new java.lang.String("item3");


var stringArray =;

getDrawableId() function

Returns the drawable id from a given resource name

var drawableId ="icon");
const drawableId: number ="icon");

getStringId() function

Returns the id of the string from the resources, while using its name

var stringId ="resource_string_name");
const stringId: string ="resource_string_name");

getId() function

Returns the id from a resource, while passing string with resource type and name. eg: :drawable/<resource_name>, :string/<resource_name>

var id ="resource_name");
const id: number ="resource_name");

getPalleteColor() function

Returns a color from the current theme using the resource color name.

var context =;
var currentThemeColor ="resource_color_name", context);
const context =;
const currentThemeColor: number ="resource_color_name", context);


jsArrayToNSArray() function

Converts a JavaScript array to a NSArray

var jsArray = ["item1", "item2", "item3"];
var nsArray = utilsModule.ios.collections.jsArrayToNSArray(jsArray);
const jsArray: Array<string> = ["item1", "item2", "item3"];
const nsArray = utils.ios.collections.jsArrayToNSArray(jsArray);

nsArrayToJSArray() function

Converts a NSArray to a JavaScript array.

var nsArray = new NSArray(["item1", "item2", "item3"]);
var jsArray = utilsModule.ios.collections.nsArrayToJSArray(nsArray);
const nsArray = new NSArray(["item1", "item2", "item3"]);
const jsArray: Array<any> = utils.ios.collections.nsArrayToJSArray(nsArray);

isLandscape() function

Returns true if current orientation is Landscape.

var value = utilsModule.ios.isLandscape();
const value: boolean = utils.ios.isLandscape()

MajorVersion() function

Returns a number with the iOS device major version(eg 8.1 will return 8).

console.log("iOS MajorVersion "+ utilsModule.ios.MajorVersion);
console.log("iOS MajorVersion "+ utils.ios.MajorVersion);

openFile() function

Opens file with associated application, while using file path.