Categories
NativeScript Vue

NativeScript Vue — Web View and Dialogs

Vue is an easy to use framework for building front end apps.

NativeScript is a mobile app framework that lets us build native mobile apps with popular front end frameworks.

In this article, we’ll look at how to build an app with NativeScript Vue.

WebView

We can add a WebView component to display web content in our app.

For example, we can write:

<template>
  <Page>
    <ActionBar title="NativeScript App"></ActionBar>
    <FlexboxLayout flexDirection="column">
      <WebView src="https://www.yahoo.com/" />
    </FlexboxLayout>
  </Page>
</template>

<script>
export default {};
</script>

We set the URL of the web page to show as the value of the src prop.

Also, we can show a local HTML page by writing:

<template>
  <Page>
    <ActionBar title="NativeScript App"></ActionBar>
    <FlexboxLayout flexDirection="column">
      <WebView src="~/assets/hello.html" />
    </FlexboxLayout>
  </Page>
</template>

<script>
export default {};
</script>

And we also can show a rendered HTML string with it:

<template>
  <Page>
    <ActionBar title="NativeScript App"></ActionBar>
    <FlexboxLayout flexDirection="column">
      <WebView src="<div><h1>hello world</h1></div>" />
    </FlexboxLayout>
  </Page>
</template>

<script>
export default {};
</script>

We set the src prop to an HTML string and it’ll be displayed.

ActionDialog

We can show an action dialog with the action method.

For example, we can write:

<template>
  <Page>
    <ActionBar title="NativeScript App"></ActionBar>
    <FlexboxLayout flexDirection="column">
      <Button text="Open Dialog" @tap="openDialog" />
    </FlexboxLayout>
  </Page>
</template>

<script>
export default {
  methods: {
    async openDialog() {
      const result = await action("Your message", "Cancel", [
        "Option1",
        "Option2",
      ]);
      console.log(result);
    },
  },
};
</script>

We add a button to call the openAlert method to open the action dialog when we tap it.

The action function is a global function that’s called to open the action dialog.

The first argument is the message, which is displayed as the title.

The 2nd argument is the cancel button text.

And the 3rd argument is an array of option texts that we can tap on.

AlertDialog

We can show an alert dialog with the alert global function.

For example, we can write:

<template>
  <Page>
    <ActionBar title="NativeScript App"></ActionBar>
    <FlexboxLayout flexDirection="column">
      <Button text="Open Dialog" @tap="openDialog" />
    </FlexboxLayout>
  </Page>
</template>

<script>
export default {
  methods: {
    async openDialog() {
      await alert({
        title: "Your title",
        message: "Your message",
        okButtonText: "OK",
      });
      console.log("Alert dialog closed");
    },
  },
};
</script>

We add the button to show the alert when we tap on the button.

The alert function takes an object that lets us set the title of the dialog.

message is the content text of the dialog.

okButtonText is the OK button’s text.

Also, we can just pass in a string to display:

<template>
  <Page>
    <ActionBar title="NativeScript App"></ActionBar>
    <FlexboxLayout flexDirection="column">
      <Button text="Open Dialog" @tap="openDialog" />
    </FlexboxLayout>
  </Page>
</template>

<script>
export default {
  methods: {
    async openDialog() {
      await alert("hello world");
      console.log("Alert dialog closed");
    },
  },
};
</script>

ConfirmDialog

The confirm global function lets us open a confirm dialog.

For example, we can write:

<template>
  <Page>
    <ActionBar title="NativeScript App"></ActionBar>
    <FlexboxLayout flexDirection="column">
      <Button text="Open Dialog" @tap="openDialog" />
    </FlexboxLayout>
  </Page>
</template>

<script>
export default {
  methods: {
    async openDialog() {
      const result = await confirm({
        title: "Your title",
        message: "Your message",
        okButtonText: "OK",
        cancelButtonText: "Cancel",
      });
      console.log(result);
    },
  },
};
</script>

We add the button to open the confirm dialog by calling the confirm function.

The title property has the title text.

message has the dialog message.

okButtonText has the OK button’s text.

And the cancelButtonText has the cancel button text.

We can also add a simple dialog by writing:

<template>
  <Page>
    <ActionBar title="NativeScript App"></ActionBar>
    <FlexboxLayout flexDirection="column">
      <Button text="Open Dialog" @tap="openDialog" />
    </FlexboxLayout>
  </Page>
</template>

<script>
export default {
  methods: {
    async openDialog() {
      const result = await confirm("Your message");
      console.log(result);
    },
  },
};
</script>

The string is the message text for the confirm dialog.

Conclusion

We can add web views and various dialogs with NativeScript Vue.

Categories
NativeScript Vue

NativeScript Vue — Text Input and Time Picker

Vue is an easy to use framework for building front end apps.

NativeScript is a mobile app framework that lets us build native mobile apps with popular front end frameworks.

In this article, we’ll look at how to build an app with NativeScript Vue.

TextField

The TextField component lets us add a text input into our app.

For example, we can use it by writing:

<template>
  <Page>
    <ActionBar title="NativeScript App"></ActionBar>
    <FlexboxLayout flexDirection="column">
      <TextField v-model="textFieldValue" />
      <Label :text="textFieldValue" style="text-align: center" />
    </FlexboxLayout>
  </Page>
</template>

<script >
export default {
  data() {
    return {
      textFieldValue: "",
    };
  },
};
</script>

We add the TextField component and add the v-model directive to bind the input value to the textFieldValue reactive property.

Then we display that value in the Label .

So when we type in something, the inputted value is shown.

We can also add the hint prop to add an input placeholder.

And the secure prop hides the entered text when it’s true .

It also takes the autocorrect prop to enable or disable autocorrect.

TextView

We can add a TextView to our NativeScript Vue app to show an editable or read-only multiline text container.

To add an editable TextView , we can write:

<template>
  <Page>
    <ActionBar title="NativeScript App"></ActionBar>
    <FlexboxLayout flexDirection="column">
      <TextView v-model="textViewValue" />
      <Label :text="textViewValue" style="text-align: center" />
    </FlexboxLayout>
  </Page>
</template>

<script >
export default {
  data() {
    return {
      textViewValue: "",
    };
  },
};
</script>

We bind the inputted value to the textViewValue with the v-model directive.

Also, we can use it to display multi-style text by writing:

<template>
  <Page>
    <ActionBar title="NativeScript App"></ActionBar>
    <FlexboxLayout flexDirection="column">
      <TextView :editable="false">
        <FormattedString>
          <Span text="You can use text attributes such as " />
          <Span text="bold, " fontWeight="Bold" />
          <Span text="italic " fontStyle="Italic" />
          <Span text="and " />
          <Span text="underline." textDecoration="Underline" />
        </FormattedString>
      </TextView>
    </FlexboxLayout>
  </Page>
</template>

<script >
export default {
  data() {
    return {
      textViewValue: "",
    };
  },
};
</script>

We set the editable prop to false to disable editing.

And then we add the FormattedString and Span components to add the styled text.

TimePicker

We can use the TimePicker component to add a time picker into our NativeScript Vue app.

For example, we can write:

<template>
  <Page>
    <ActionBar title="NativeScript App"></ActionBar>
    <FlexboxLayout flexDirection="column">
      <TimePicker v-model="selectedTime" />
      <Label :text="selectedTime" style="text-align: center" />
    </FlexboxLayout>
  </Page>
</template>

<script >
export default {
  data() {
    return {
      selectedTime: undefined,
    };
  },
};
</script>

We bind the selected time value to the selectedTime reactive property with v-model .

And we display the selected item with the Label .

We can set the following props to configure the TimePicker :

  • hourNumber — gets or sets the selected hour.
  • minuteNumber — gets or sets the selected minute.
  • timeDate — gets or sets the selected time.
  • minHourNumber — gets or sets the minimum selectable hour.
  • maxHourNumber — gets or sets the maximum selectable hour.
  • minMinuteNumber — gets or sets the minimum selectable minute.
  • maxMinuteNumber — gets or sets the maximum selectable minute.
  • minuteIntervalNumber — gets or sets the selectable minute interval.

Conclusion

We can add various kinds of input controls into our NativeScript Vue mobile app.

Categories
NativeScript Vue

NativeScript Vue — Navigation, Toggles, and Sliders

Vue is an easy to use framework for building front end apps.

NativeScript is a mobile app framework that lets us build native mobile apps with popular front end frameworks.

In this article, we’ll look at how to build an app with NativeScript Vue.

SegmentedBar

The SegmentedBar component lets us display a set of buttons and let us select a choice by clicking one.

For example, we can write:

<template>
  <Page>
    <ActionBar title="NativeScript App"></ActionBar>
    <FlexboxLayout flexDirection="column">
      <SegmentedBar>
        <SegmentedBarItem title="First" />
        <SegmentedBarItem title="Second" />
        <SegmentedBarItem title="Third" />
      </SegmentedBar>
    </FlexboxLayout>
  </Page>
</template>

<script >
export default {};
</script>

to add the SegmentedBar .

The flexDirection is set to 'column' so that the SegmenteBarItem s are displayed side by side.

We can also bind the selected bar item’s index to a reactive property with the v-model directive:

<template>
  <Page>
    <ActionBar title="NativeScript App"></ActionBar>
    <FlexboxLayout flexDirection="column">
      <SegmentedBar v-model="selectedItem">
        <SegmentedBarItem
          v-for="item in listOfItems"
          :key="item"
          :title="item.title"
        />
      </SegmentedBar>
      <Label :text="selectedItem" style="text-align: center" />
    </FlexboxLayout>
  </Page>
</template>

<script >
export default {
  data() {
    return {
      listOfItems: [
        { title: "apple" },
        { title: "orange" },
        { title: "grape" },
      ],
      selectedItem: 0,
    };
  },
};
</script>

We use v-model to bind the selectedItem reactive property to the selected index.

So when we click on an item, we’ll see the index of it displayed in the Label .

Slider

The Slider component is a UI component that shows a slider control for picking values in a specified numeric range.

For example, we can write:

<template>
  <Page>
    <ActionBar title="NativeScript App"></ActionBar>
    <FlexboxLayout flexDirection="column">
      <Slider v-model="value" />
      <Label :text="value" style="text-align: center" />
    </FlexboxLayout>
  </Page>
</template>

<script >
export default {
  data() {
    return {
      value: 0,
    };
  },
};
</script>

to add the numeric slider and bind the selected value to the value reactive property.

We can set the minValue and maxValue props to set the min and max values that we can choose respectively.

The default values are 0 and 100 respectively.

Switch

Then Switch component lets us add a toggle switch in our app.

For instance, we can use it by writing:

<template>
  <Page>
    <ActionBar title="NativeScript App"></ActionBar>
    <FlexboxLayout flexDirection="column">
      <Switch v-model="itemEnabled" />
      <Label :text="itemEnabled" style="text-align: center" />
    </FlexboxLayout>
  </Page>
</template>

<script >
export default {
  data() {
    return {
      itemEnabled: false,
    };
  },
};
</script>

We bind the toggle switch’s value to the itemEnabled reactive property with v-model .

And the selected value is displayed in the Label .

TabView

The TabView is a navigation component that shows content grouped into tabs and lets users switch between them.

For example, we can use it by writing:

<template>
  <Page>
    <ActionBar title="NativeScript App"></ActionBar>
    <FlexboxLayout flexDirection="column">
      <TabView
        :selectedIndex="selectedIndex"
        @selectedIndexChange="indexChange"
      >
        <TabViewItem title="Tab 1">
          <Label text="Content for Tab 1" />
        </TabViewItem>
        <TabViewItem title="Tab 2">
          <Label text="Content for Tab 2" />
        </TabViewItem>
      </TabView>
    </FlexboxLayout>
  </Page>
</template>

<script >
export default {
  data() {
    return {
      selectedIndex: 0,
    };
  },
  methods: {
    indexChange({ value: newIndex }) {
      this.selectedIndex = newIndex;
      console.log(newIndex);
    },
  },
};
</script>

We add the TabView and set the selectedIndex reactive property to the value of the value property in the parameter object when the selectedIndexChange event is emitted.

The tab content is rendered by the TabViewItem component.

The Label s are the content.

Conclusion

We can add a segmented bar and tab view to add navigation into our NativeScript Vue mobile app.

Numeric slider and toggles are also provided by NativeScript Vue.

Categories
NativeScript Vue

NativeScript Vue — Page, Progress Bar, Scroll View, and Search Bar

Vue is an easy to use framework for building front end apps.

NativeScript is a mobile app framework that lets us build native mobile apps with popular front end frameworks.

In this article, we’ll look at how to build an app with NativeScript Vue.

Page

The Page component lets us render the app’s screen.

It’s a wrapper for one or more components.

For example, we can write:

<template>
  <Page>
    <ActionBar title="NativeScript App"></ActionBar>
    <FlexboxLayout flexDirection="column">
      <Label text="Foo" />
      <Label text="Bar" />
    </FlexboxLayout>
  </Page>
</template>

<script >
export default {};
</script>

to add the labels to the page.

Placeholder

The Placeholder component lets us add native widgets into our app.

For example, we can write:

<template>
  <Page>
    <ActionBar title="NativeScript App"></ActionBar>
    <FlexboxLayout flexDirection="column">
      <Placeholder @creatingView="creatingView" />
    </FlexboxLayout>
  </Page>
</template>

<script >
export default {
  methods: {
    creatingView(args) {
      const nativeView = new android.widget.TextView(args.context);
      nativeView.setSingleLine(true);
       nativeView.setEllipsize(android.text.TextUtils.TruncateAt.END);
      nativeView.setText("Hello World");
      args.view = nativeView;
    },
  },
};
</script>

We listen to the creatingView event and run the creatingView method when it’s emitted.

Then we create the text view with the android.widget.TextView constructor.

We pass in the args.context property to return the native view.

Then we call the setEllipseize to set the ellipsis for the text view.

And we call setText to set the text for the text view.

And we set that as the value of the args.view property to set the view.

Progress

The Progress component lets us show a bar to show the progress of a task.

For example, we can write:

<template>
  <Page>
    <ActionBar title="NativeScript App"></ActionBar>
    <FlexboxLayout flexDirection="column">
      <Progress :value="currentProgress" />
    </FlexboxLayout>
  </Page>
</template>

<script >
export default {
  data() {
    return {
      currentProgress: 50,
    };
  },
};
</script>

We add the Progress component to show the progress bar.

The value prop has the progress value. It can be between 0 and 100.

ScrollView

The ScrollView component lets us add a scrollable content area into our app.

For example, we can write:

<template>
  <Page>
    <ActionBar title="NativeScript App"></ActionBar>
    <FlexboxLayout flexDirection="column">
      <ScrollView orientation="horizontal">
        <StackLayout orientation="horizontal">
          <Label :text="n" v-for="n in 100" :key="n" width='30' />
        </StackLayout>
      </ScrollView>
    </FlexboxLayout>
  </Page>
</template>

<script >
export default {};
</script>

to add a ScrollView with a StackLayout inside.

We set both orientation props to 'horizontal' to display a horizontal scroll view.

Then we add the Label inside the StackLayout to display the numbers.

Now we can scroll through the numbers.

SearchBar

The SearchBar component lets us add an input box to let users enter a search query.

For instance, we can write:

<template>
  <Page>
    <ActionBar title="NativeScript App"></ActionBar>
    <FlexboxLayout flexDirection="column">
      <SearchBar v-model="searchQuery" @submit="onSubmit" />
    </FlexboxLayout>
  </Page>
</template>

<script >
export default {
  data() {
    return {
      searchQuery: "",
    };
  },
  methods: {
    onSubmit() {
      alert(this.searchQuery);
    },
  },
};
</script>

We bind the input value of the SearchBar to the searchQuery reactive property.

Then when we press Enter, the submit event is emitted.

Then the onSubmit method is called.

We can add a search hint with the hint prop.

Conclusion

We can add a page, progress bar, scroll view, and search bar into our mobile app with NativeScript Vue.

Categories
NativeScript Vue

NativeScript Vue — Labels, ListViews, and List Pickers

Vue is an easy to use framework for building front end apps.

NativeScript is a mobile app framework that lets us build native mobile apps with popular front end frameworks.

In this article, we’ll look at how to build an app with NativeScript Vue.

Label

We can add a label to our app with the Label component.

For example, we can write:

<template>
  <Page>
    <ActionBar title="NativeScript App"></ActionBar>
    <FlexboxLayout flexDirection="column">
      <Label text="Label" />
    </FlexboxLayout>
  </Page>
</template>

<script >
export default {};
</script>

to add a label into our app.

The text prop has the text to display.

We can also add formatted text with:

<template>
  <Page>
    <ActionBar title="NativeScript App"></ActionBar>
    <FlexboxLayout flexDirection="column">
      <Label textWrap>
        <FormattedString>
          <Span
            text="hello world"
            fontWeight="bold"
            fontStyle="italic"
            style="color: red"
          />
        </FormattedString>
      </Label>
    </FlexboxLayout>
  </Page>
</template>

<script >
export default {};
</script>

We add the FormattedString component to add the formatted string.

The Span has the text, font weight, font style, and other styles we want to add.

ListPicker

We can add a component to let users pick a choice with the ListPicker component.

For example, we can write:

<template>
  <Page>
    <ActionBar title="NativeScript App"></ActionBar>
    <FlexboxLayout flexDirection="column">
      <ListPicker
        :items="listOfItems"
        selectedIndex="0"
        @selectedIndexChange="selectedIndexChanged"
      />
    </FlexboxLayout>
  </Page>
</template>

<script >
export default {
  data() {
    return {
      listOfItems: ["apple", "orange", "grape"],
    };
  },
  methods: {
    selectedIndexChanged({ value }) {
      console.log(value);
    },
  },
};
</script>

We add the ListPicker component with the items prop to set the items displayed.

selectedIndex is set to 0 to select the first item by default.

And we listen to the selectedIndexChange event that’s emitted from the component.

We can get the index of the selected component with the value prop.

We can shorten this with the v-model directive:

<template>
  <Page>
    <ActionBar title="NativeScript App"></ActionBar>
    <FlexboxLayout flexDirection="column">
      <ListPicker :items="listOfItems" v-model="selectedItem" />
      <Label :text="selectedItem" style="text-align: center" />
    </FlexboxLayout>
  </Page>
</template>

<script >
export default {
  data() {
    return {
      listOfItems: ["apple", "orange", "grape"],
      selectedItem: "",
    };
  },
};
</script>

It binds the index of the selected item to the selectedItem reactive property.

ListView

We can add a vertically scrolling list view with the ListView component.

For example, we can write:

<template>
  <Page>
    <ActionBar title="NativeScript App"></ActionBar>
    <FlexboxLayout flexDirection="column">
      <ListView for="item in listOfItems" [@itemTap](https://medium.com/r/?url=http%3A%2F%2Ftwitter.com%2FitemTap "Twitter profile for @itemTap")="onItemTap">
        <v-template>
          <Label :text="item.text" />
        </v-template>
      </ListView>
    </FlexboxLayout>
  </Page>
</template>

<script >
export default {
  data() {
    return {
      listOfItems: [
        {
          text: "apple",
        },
        { text: "orange" },
        {
          text: "grape",
        },
      ],
      selectedItem: "",
    };
  },
  methods: {
    onItemTap({ item }) {
      console.log(item);
    },
  },
};
</script>

to add the ListView component to display our objects in our code.

We add a Label into the default slot to display the items the way we like.

The itemTap event is emitted when we tap on an item.

Then we can get the tapped item with the onItemTap method.

Also, we can add multiple v-template blocks.

For example, we can write:

<template>
  <Page>
    <ActionBar title="NativeScript App"></ActionBar>
    <FlexboxLayout flexDirection="column">
      <ListView for="item in listOfItems" @itemTap="onItemTap">
        <v-template>
          <Label :text="item.text" />
        </v-template>

        <v-template if="$odd">
          <Label :text="item.text" color="red" />
        </v-template>
      </ListView>
    </FlexboxLayout>
  </Page>
</template>

<script >
export default {
  data() {
    return {
      listOfItems: [
        {
          text: "apple",
        },
        { text: "orange" },
        {
          text: "grape",
        },
      ],
      selectedItem: "",
    };
  },
  methods: {
    onItemTap({ item }) {
      console.log(item);
    },
  },
};
</script>

We add the if prop to the v-template to show something different for items with an odd index with the $odd reactive property.

Also, we can use $even to for check if an item has an even index.

$index has the index of the item.

ListView doesn’t loop through the list items like we expect with v-for .

It just creates the required views to display the currently visible items on the screen.

The views are reused for items that were off-screen and now shown on-screen.

Conclusion

We can add labels, list pickers, and list views into our mobile app with NativeScript Vue.