MadNeox
MadNeox

Reputation: 2485

Unable to find android.view in NativeScript

I am developing a native script app using angular, and I need to access the native Android API. I tried to use android.view as mentioned in the native script documentation, but I get an error saying that view is not defined : Native Script documentation

Here below is the declaration used to access the android object.

import { android } from "application";

Can you help me please to find out why am I getting this error ?

Upvotes: 2

Views: 558

Answers (3)

Ricky Levi
Ricky Levi

Reputation: 7987

In the answer ( which also answered my issue ) there are two things to note:

  • The native variable that you can use android
  • The one that you import from import { android } from "@nativescript/core/application";

when I used as the answer suggested I run into an issue where it now couldn't find the addEventListener in code: android.addEventListener

So eventually I wrote something like this to differentiate between these two variables

import {
  android as Android,         <= aliased android => Android
  AndroidActivityEventData,
  AndroidApplication } from '@nativescript/core/application';

Android.addEventListener(AndroidApplication.activityCreatedEvent, (event: AndroidActivityEventData) => {
  event.activity.getWindow().getDecorView().setLayoutDirection(android.view .View.LAYOUT_DIRECTION_RTL);
});

Upvotes: 0

MadNeox
MadNeox

Reputation: 2485

As per Brad Martin response, there is no need to make any import. To use it in angular environment, add this line before the component declaration.

declare var android :any;

Upvotes: 0

Brad Martin
Brad Martin

Reputation: 6147

You don't have to import anything to access the native APIs. So to use android.view you'd just call it in your app like this:

const x = new android.view.ViewGroup.... // or whatever you're accessing

Upvotes: 3

Related Questions