Changing A NativeScript CSS Skin At Runtime

CSS is usually a subject I avoid due to me being artistically illiterate, but recently a student of mine asked me an interesting question regarding theming an Angular 2 NativeScript application with dynamic CSS files loaded at runtime.  Given the nature of Angular 2, it becomes difficult to load files at runtime because of how Angular 2 compiles and builds projects.  NativeScript Angular 2 projects are no exception when it comes to switching a CSS skin.

So what if we want to apply a set of CSS styles on demand, but keep them separated in their own files?

We’re going to see how to switch between files to apply a CSS skin to a NativeScript Angular 2 application on demand at runtime.

To make things easy to understand we’re going to start with a fresh project.  From the Command Prompt (Windows) or Terminal (Linux and Mac), execute the following commands:

The --ng flag in the above indicates that we are creating an Angular 2 TypeScript project.  Although I’m adding the iOS build platform, we cannot actually build for iOS unless we’re using a Mac.

To make switching CSS files at runtime possible, we need to use the nativescript-themes plugin by Nathanael Anderson.  Execute the following to install the plugin into your project:

This plugin is not to be confused with the NativeScript theme that is provided to you at project creation where that is a single theme, not a theme switcher plugin.

Defining our CSS Themes

Before we jump into our application logic and UI, let’s define various CSS themes that can be switched between at application runtime.

Create an app/red-theme.css file and include the following CSS:

You can see in the above CSS file that there isn’t anything too complex.  Remember, this is only an example.  Essentially we’re going to change the color of the application action bar and the color of the buttons to shades of red.

Now create an app/blue-theme.css file and include the following CSS:

The above file contains the same CSS, but different colors.  This time we’re using shades of blue.  For simplicity we’re only going to be switching between two CSS skins.

Adding the TypeScript Logic for Applying a CSS Skin

With the skin files in place, we need a way to apply them via our Angular 2 TypeScript code.  Open the project’s app/app.component.ts file and include the following TypeScript code:

To keep things easy to understand, let’s break down what is happening in the above code.

We start things off by importing a standard Angular 2 component as well as our installed nativescript-themes plugin.

The constructor method is where we define the starting CSS skin:

As you can see in the above code, we’re starting with the app/red-theme.css file if no other theme was already selected as the default theme.

The applyBlueTheme and applyRedTheme accomplish pretty much the same things:

Each of the two methods applies either the app/blue-theme.css or app/red-theme.css files without checking for a default theme.

So what does the UI behind this TypeScript look like?

Creating the Application UI

The UI behind this application is going to be short and sweet.  We are going to have an action bar with two buttons.  When the theme changes, so will the colors of the action bar and the two buttons.

Open the project’s app/app.component.html file and include the following HTML markup:

The tap events trigger each of the methods for switching between theme colors.


You just saw how to change CSS skins at application runtime in a NativeScript Angular 2 Android and iOS mobile application.  This is useful if you want to your users to select a theme or maybe you want to apply a day-time theme and a night-time theme, sort of how navigation systems work in your car.  In that scenario you would have better visibility as needed.  Too many scenarios to list where this would be beneficial.

Nic Raboy

Nic Raboy is an advocate of modern web and mobile development technologies. He has experience in Java, JavaScript, Golang and a variety of frameworks such as Angular, NativeScript, and Apache Cordova. Nic writes about his development experiences related to making web and mobile development easier to understand.

  • angelo

    Thank you for sharing Nic, you’re an amazing instructor.
    p.s. I follow your nativescript for the angular 2 developer course on Udemy and I’ve read that blog at, and well… I’m learning a lot from you. so, Thanks man. I appreciate it 🙂

  • criticalthinker

    good stuff, as usual! thanks nic 🙂 also could be very useful in dynamically styled whitelabel projects i think