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.

Categories
NativeScript Vue

NativeScript Vue — Date Picker, Frame, HtmlView, and Images

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.

DatePicker

NativeScript Vue comes with a date picker component.

To use it, we add:

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

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

We add the DatePicker with the v-model directive to bind the selected value to the selectedDate reactive property.

Also, we can restrict the date range that we can choose with the minDate and maxDate props.

They’ll set the min and max dates that we can choose.

date get or set the complete date.

And day , month , and year gets or sets the day, month, and year respectively.

Frame

The Frame component is the root element for our app.

Every app needs at least one Frame .

We can have also more than one Frame.

For example, in main.js , we have:

import Vue from 'nativescript-vue'
import App from './components/App'
import VueDevtools from 'nativescript-vue-devtools'

if(TNS_ENV !== 'production') {
  Vue.use(VueDevtools)
}

// Prints Vue logs when --env.production is *NOT* set while building
Vue.config.silent = (TNS_ENV === 'production')

new Vue({

  render: h => h('frame', [h(App)])
}).$start()

to render the frame.

HtmlView

We can add the HtmlView component to show static HTML content in our NativeScript Vue app.

For example, we can write:

<template>
  <Page>
    <ActionBar title="NativeScript App"></ActionBar>
    <FlexboxLayout flexDirection="column">
      <HtmlView html="<div><h1>HtmlView</h1></div>" />
    </FlexboxLayout>
  </Page>
</template>

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

to add the HtmlView to display the raw HTML in the html prop.

Image

The Image component lets us show images.

For example, we can write:

<template>
  <Page>
    <ActionBar title="NativeScript App"></ActionBar>
    <FlexboxLayout flexDirection="column">
      <Image
        src="https://i.picsum.photos/id/23/200/200.jpg?hmac=IMR2f77CBqpauCb5W6kGzhwbKatX_r9IvgWj6n7FQ7c"
        stretch="none"
      />
    </FlexboxLayout>
  </Page>
</template>

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

We set the src prop to the URL of the image.

stretch is set to 'none' to disable stretching.

We can also the src to a path in the project. For instance, we can write:

<template>
  <Page>
    <ActionBar title="NativeScript App"></ActionBar>
    <FlexboxLayout flexDirection="column">
      <Image src="~/assets/images/NativeScript-Vue.png" stretch="none" />
    </FlexboxLayout>
  </Page>
</template>

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

We can reference an image in the /src/assets/images folder with this path.

Also, we can reference an icon:

<template>
  <Page>
    <ActionBar title="NativeScript App"></ActionBar>
    <FlexboxLayout flexDirection="column">
      <Image src="res://icon" stretch="none" />
    </FlexboxLayout>
  </Page>
</template>

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

A base64 string also works:

<template>
  <Page>
    <ActionBar title="NativeScript App"></ActionBar>
    <FlexboxLayout flexDirection="column">
      <Image
        src="data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/4QDeRXhpZgAASUkqAAgAAAAGABIBAwABAAAAAQAAABoBBQABAAAAVgAAABsBBQABAAAAXgAAACgBAwABAAAAAgAAABMCAwABAAAAAQAAAGmHBAABAAAAZgAAAAAAAAA4YwAA6AMAADhjAADoAwAABwAAkAcABAAAADAyMTABkQcABAAAAAECAwCGkgcAFQAAAMAAAAAAoAcABAAAADAxMDABoAMAAQAAAP//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/AIBDXTXzEfidP//Z"
        stretch="none"
      />
    </FlexboxLayout>
  </Page>
</template>

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

We can also display a font icon:

<template>
  <Page>
    <ActionBar title="NativeScript App"></ActionBar>
    <FlexboxLayout flexDirection="column">
      <Image src.decode="font://&#xf004;" class="fas" />
    </FlexboxLayout>
  </Page>
</template>

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

We add the decode modifier to decode the icon font.

Conclusion

We can add a date picker, HTML view, frame and an image into our app with NativeScript Vue.

Categories
NativeScript Vue

NativeScript Vue — Toolbars, Progress Indicators, and Buttons

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.

ActionItem

We can use the ActionItem component to add action buttons into the ActionBar component.

For example, we can write:

<template>
  <Page>
    <ActionBar title="NativeScript App">
      <ActionItem
        @tap="onTapShare"
        android.systemIcon="ic_menu_share"
        android.position="actionBar"
      />
      <ActionItem @tap="onTapDelete" text="delete" android.position="popup" />
    </ActionBar>
    <WrapLayout></WrapLayout>
  </Page>
</template>

<script >
export default {
  methods: {
    onTapShare() {
      console.log("shared tapped");
    },
    onTapDelete() {
      console.log("delete tapped");
    },
  },
};
</script>

The first ActionItem is an icon that’s displayed on the bar.

We set the icon to display with the android.systemIcon prop.

The android.position prop sets the location that the icon is displayed in.

The 2nd ActionItem has the text prop for the menu item text.

And we set the android.position to 'popup' so that we display the ‘delete’ item in the popup menu on the top right corner.

The tap event listeners are run when we tap of the buttons.

NavigationButton

The NavigationButton component lets us add navigation buttons into our app.

For example, we can write:

<template>
  <Page>
    <ActionBar title="NativeScript App">
      <NavigationButton
        text="Go back"
        android.systemIcon="ic_menu_back"
        @tap="goBack"
      />
    </ActionBar>
    <WrapLayout></WrapLayout>
  </Page>
</template>

<script >
export default {
  methods: {
    goBack() {
      console.log("go back tapped");
    },
  },
};
</script>

We add the NavigationButton into our ActionBar .

We set the text for the navigation button, which is displayed beside the icon.

android.systemIcon sets the icon.

We have the tap event listener on the NavigationItem , so the goBack method is run when we tap on the navigation button.

ActivityIndicator

The ActivityIndicator component lets us show a progress indicator for the user.

For example, we can write:

<template>
  <Page>
    <ActionBar title="NativeScript App"> </ActionBar>
    <WrapLayout>
      <ActivityIndicator :busy="busy" @busyChange="onBusyChanged" />
    </WrapLayout>
  </Page>
</template>

<script >
export default {
  data() {
    return { busy: false };
  },
  mounted() {
    this.busy = true;
    setTimeout(() => {
      this.busy = false;
    }, 3000);
  },
  methods: {
    onBusyChanged() {
      console.log("busy changed");
    },
  },
};
</script>

We add the ActivityIndicator to our app.

The busy prop controls when the activity indicator is displayed.

It’ll be displayed if it’s true .

Also, we listen to the busyChange event with the onBusyChanged method.

It runs whenever the busy prop is changed.

Button

We can use the Button component to add a button into the screen.

To do this, we write:

<template>
  <Page>
    <ActionBar title="NativeScript App"></ActionBar>
    <FlexboxLayout
      justifyContent="space-around"
      flexDirection="column"
      backgroundColor="#3c495e"
    >
      <Button text="Button" @tap="onButtonTap" />
    </FlexboxLayout>
  </Page>
</template>

<script >
export default {
  methods: {
    onButtonTap() {
      console.log("button tapped");
    },
  },
};
</script>

We add the Button component with the text prop to set the button text.

And we listen to the tap event with the onButtonTap method.

We can style our button with the Span component.

For example, we can write:

<template>
  <Page>
    <ActionBar title="NativeScript App"></ActionBar>
    <FlexboxLayout
      justifyContent="space-around"
      flexDirection="column"
      backgroundColor="#3c495e"
    >
      <Button @tap="onButtonTap">
        <Span text="Red and Bold" fontWeight="bold" style="color: red" />
      </Button>
    </FlexboxLayout>
  </Page>
</template>

<script >
export default {
  methods: {
    onButtonTap() {
      console.log("button tapped");
    },
  },
};
</script>

We add the Span inside the Button .

The text prop sets the button text.

fontWeight sets the font-weight.

The style prop sets the text style.

Conclusion

We can add top bar items, progress indicators and buttons with NativeScript Vue.

Categories
NativeScript Vue

NativeScript Vue — Stack and Wrap Layouts and Action Bars

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.

Stack Layout with Horizontally Aligned Children

We can add stack layouts with horizontally aligned the children components.

For example, we can write:

<template>
  <Page>
    <ActionBar title="Welcome to NativeScript-Vue!" />
    <StackLayout backgroundColor="#3c495e">
      <Label
        text="left"
        horizontalAlignment="left"
        width="33%"
        height="70"
        backgroundColor="red"
      />
      <Label
        text="center"
        horizontalAlignment="center"
        width="33%"
        height="70"
        backgroundColor="green"
      />
      <Label
        text="right"
        horizontalAlignment="right"
        width="33%"
        height="70"
        backgroundColor="blue"
      />
      <Label
        text="stretch"
        horizontalAlignment="stretch"
        height="70"
        backgroundColor="yellow"
      />
    </StackLayout>
  </Page>
</template>

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

We set the horizontalAlignment prop to the position we want to place the Label in.

Setting to 'stretch' will make the Label span the whole screen’s width.

Also, we can make child components vertically aligned.

For example, we can write:

<template>
  <Page>
    <ActionBar title="Welcome to NativeScript-Vue!" />
    <StackLayout orientation="horizontal" backgroundColor="#3c495e">
      <Label
        text="top"
        verticalAlignment="top"
        width="70"
        height="33%"
        backgroundColor="red"
      />
      <Label
        text="center"
        verticalAlignment="center"
        width="70"
        height="33%"
        backgroundColor="green"
      />
      <Label
        text="bottom"
        verticalAlignment="bottom"
        width="70"
        height="33%"
        backgroundColor="blue"
      />
      <Label
        text="stretch"
        verticalAlignment="stretch"
        width="70"
        backgroundColor="yellow"
      />
    </StackLayout>
  </Page>
</template>

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

We set the orientation to 'horizontal' to vertically align the Label s.

Then in the Label s, we set the verticalAlignment prop instead of horizontalAlignment .

WrapLayout

The WrapLayout component lets us position items in rows or columns.

We can add a row of equally-sized items and wrap any overflowing items by default.

For example, we can write:

<template>
  <Page>
    <ActionBar title="Welcome to NativeScript-Vue!" />
    <WrapLayout backgroundColor="#3c495e">
      <Label text="first" width="30%" height="30%" backgroundColor="red" />
      <Label text="second" width="30%" height="30%" backgroundColor="green" />
      <Label text="third" width="30%" height="30%" backgroundColor="blue" />
      <Label text="fourth" width="30%" height="30%" backgroundColor="yellow" />
    </WrapLayout>
  </Page>
</template>

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

to add the WrapLayout with Label s inside.

We can change the orientation prop to 'vertical' to wrap overflowing items into a new column.

To do this, we write:

<template>
  <Page>
    <ActionBar title="Welcome to NativeScript-Vue!" />
    <WrapLayout orientation="vertical" backgroundColor="#3c495e">
      <Label text="first" width="30%" height="30%" backgroundColor="red" />
      <Label text="second" width="30%" height="30%" backgroundColor="green" />
      <Label text="third" width="30%" height="30%" backgroundColor="blue" />
      <Label text="fourth" width="30%" height="30%" backgroundColor="yellow" />
    </WrapLayout>
  </Page>
</template>

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

ActionBar

We can use the ActionBar component to add a toolbar at the top of the activity window.

For example, we can write:

<template>
  <Page>
    <ActionBar title="Welcome to NativeScript-Vue!" />
    <WrapLayout></WrapLayout>
  </Page>
</template>

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

to add the ActionBar component to the top of the screen.

The title prop has the text that’s displayed at the top bar.

Also, we can add more items into the ActionBar :

<template>
  <Page>
    <ActionBar>
      <StackLayout orientation="horizontal">
        <Image
          src="res://icon"
          width="40"
          height="40"
          verticalAlignment="center"
        />
        <Label text="NativeScript" fontSize="24" verticalAlignment="center" />
      </StackLayout>
    </ActionBar>
    <WrapLayout></WrapLayout>
  </Page>
</template>

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

We set the verticalAlignment to 'center' to center the image vertically.

And we add the Label with the text to do the same.

We can add the app icon by setting the props for ActioBar :

<template>
  <Page>
    <ActionBar
      title="NativeScript App"
      android.icon="res://icon"
      android.iconVisibility="always"
    >
    </ActionBar>
    <WrapLayout></WrapLayout>
  </Page>
</template>

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

We set the android.icon and android.iconVisible props to add the icon and make it visible.

Also, we can remove the border by setting the flat prop to 'true' :

<template>
  <Page>
    <ActionBar title="NativeScript App" flat="true" />
    <WrapLayout></WrapLayout>
  </Page>
</template>

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

Conclusion

We can add stack and wrap layouts, and a top bar to our mobile app with NativeScript Vue.