title | description | position | slug |
---|---|---|---|
Utils |
Learn how to use different utils methods provided in the Utils core module |
100 |
utils |
To use the functionality provided by Utils
:
const Core = require("@nativescript/core");```
```TypeScript
import { Utils } from "@nativescript/core";
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 structurefalse
- if the path is not a file path
var path = "res://icon";
var value = Core.Utils.isFileOrResourcePath(path);
const path: string = "res://icon";
const value: boolean = Utils.isFileOrResourcePath(path);
Checks if the specified URI is a data URI.
- Returns
true
if the string is data URL, otherwisefalse
var url = "<url>";
var value = Core.Utils.isDataURI(url);
const url: string = "<url>";
const value:boolean = Utils.isDataURI(url);
Open an URL on device with the default browser
Core.Utils.openUrl("https://docs.nativescript.org/core-concepts/utils")
Utils.openUrl("https://docs.nativescript.org/core-concepts/utils")
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 = Core.Utils.escapeRegexSymbols(sampleString);
var sampleString: string = "All of these should be escaped: ^ $ * ";
var newString: string = Utils.escapeRegexSymbols(sampleString);
Converts a string value to a number or boolean;
var stringToBoolean = "true";
var booleanValue = Core.Utils.convertString(stringToBoolean);
var stringToNumber = "23";
var numberValue = Core.Utils.convertString(stringToNumber);
const stringToBoolean :string = "true";
const booleanValue :boolean = Utils.convertString(stringToBoolean);
const stringToNumber: string = "23";
const numberValue :number = Utils.convertString(stringToNumber);
Returns the display density of the device.
var displayDensity = Core.Utils.layout.getDisplayDensity();
const displayDensity = Utils.layout.getDisplayDensity();
Converts value from device independent pixels to device pixels.
var devicePixels = Core.Utils.layout.toDevicePixels(<dip>);
const devicePixels = Utils.layout.toDevicePixels(<dip>);
Convert value to device independent pixels.
var deviceIndependentPixels = Core.Utils.layout.toDeviceIndependentPixels(<px>);
const deviceIndependentPixels = Utils.layout.toDeviceIndependentPixels(<px>);
Rounds value used in layout.
var value = Core.Utils.layout.round(<number_value>);
var value = Utils.layout.round(<number_value>);
If we set
123.56px
as a input the returned value will be124px
.
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.
Core.Utils.executeOnMainThread(() => {
// ...
})
Utils.executeOnMainThread(() => {
// ...
})
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.
Core.Utils.mainThreadify(() => {
// ...
})
Utils.mainThreadify(() => {
// ...
})
Returns an instance of native Android application The returned value will be of type android.app.Application
.
var application = Utils.android.getApplication();
const application = Utils.android.getApplication();
Returns the Android application context. The returned value will be of type android.content.Context
.
var context = Utils.android.getApplicationContext();
const context = Utils.android.getApplicationContext();
Returns an instance of native Android input method manager. The returned value will be an
android.view.inputmethod.InputMethodManager
var inputMethodManager = Utils.android.getInputMethodManager();
const inputMethodManager = Utils.android.getInputMethodManager();
Show keyboard for a specific element.
<TextField id="textfieldid" hint="Target field" />
<Button text="Show keyboard" tap="showKeyboard" class="btn btn-primary btn-active"/>
export function showKeyboard(args) {
var button = args.object;
var page = button.page;
var textField = page.getViewById("textfieldid");
Utils.android.showSoftInput(textField.android);
}
import { Page, TextField, Button, Utils } from "@nativescript/core";
export function showKeyboard(args: EventData) {
var button: Button = <Button>args.object;
var page: Page = <Page>button.page;
var textField: TextField = <TextField> page.getViewById("textfieldid");
Utils.android.showSoftInput(textField.android);
}
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"/>
export function dismissSoftInput(args) {
Utils.android.dismissSoftInput();
}
export function dismissSoftInput(args: EventData) {
Utils.android.dismissSoftInput();
}
Converts a string array into a String hash set.
var stringArr = ["a", "b", "c"]
var stringSet = Utils.android.collections.stringArrayToStringSet(stringArr);
const stringArr = ["a", "b", "c"]
const stringSet = Utils.android.collections.stringArrayToStringSet(stringArr);
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");
hashSet.add(string1);
hashSet.add(string2);
hashSet.add(string3);
var stringArray = Utils.android.collections.stringSetToStringArray(hashSet);
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");
hashSet.add(string1);
hashSet.add(string2);
hashSet.add(string3);
var stringArray = Utils.android.collections.stringSetToStringArray(hashSet);
Returns the drawable id from a given resource name
var drawableId = Utils.android.resources.getDrawableId("icon");
const drawableId: number = Utils.android.resources.getDrawableId("icon");
Returns the id of the string from the resources, while using its name
var stringId = Utils.android.resources.getStringId("resource_string_name");
const stringId: string = Utils.android.resources.getStringId("resource_string_name");
Returns the id from a resource, while passing string with resource type and name. eg: :drawable/<resource_name>
, :string/<resource_name>
var id = Utils.android.resources.getId("resource_name");
const id: number = Utils.android.resources.getId("resource_name");
Returns a color from the current theme using the resource color name.
var context = Utils.android.getApplicationContext();
var currentThemeColor = Utils.android.resources.getPalleteColor("resource_color_name", context);
const context = Utils.android.getApplicationContext();
const currentThemeColor: number = Utils.android.resources.getPalleteColor("resource_color_name", context);
Converts a JavaScript array to a NSArray
var jsArray = ["item1", "item2", "item3"];
var nsArray = Utils.ios.collections.jsArrayToNSArray(jsArray);
const jsArray: Array<string> = ["item1", "item2", "item3"];
const nsArray = Utils.ios.collections.jsArrayToNSArray(jsArray);
Converts a NSArray to a JavaScript array.
var nsArray = new NSArray(["item1", "item2", "item3"]);
var jsArray = Utils.ios.collections.nsArrayToJSArray(nsArray);
const nsArray = new NSArray(["item1", "item2", "item3"]);
const jsArray: Array<any> = Utils.ios.collections.nsArrayToJSArray(nsArray);
Returns a number
with the iOS device major version(eg 8.1 will return 8).
console.log("iOS MajorVersion "+ Utils.ios.MajorVersion);
console.log("iOS MajorVersion "+ Utils.ios.MajorVersion);
Opens file with associated application, while using file path.
Utils.ios.openFile(<file_path>);
Utils.ios.openFile(<file_path>);