Simon Bisson
Contributing Writer

Write handsome, hybrid mobile apps with Ionic

how-to
Jul 23, 20156 mins
HTMLJavaScriptMobile Development

Cross-platform mobile dev tools ensure your app works on multiple devices, but for a stylish and functional UI, check out the Ionic JavaScript framework

mobile apps smartphone
Credit: Shutterstock

Everyone wants the apps they write to run on multiple platforms, but no one wants to give up the native features of mobile devices. Thatโ€™s why you find so many tools to create hybrid apps, where core HTML5 and JavaScript code is packaged to run on Android,ย iOS, Windows Phone โ€” even on legacy BlackBerry and Symbian devices.

With technologies like Apache Cordova, itโ€™s easy to access hardware-specific features from your JavaScript code before wrapping apps and delivering them to your chosen app stores. Theyโ€™re useful tools, but while theyโ€™re focused on getting apps to work well on mobile hardware, theyโ€™re not great for giving your app a modern look and feel.

Thatโ€™s whereย Ionic,ย a relatively new JavaScript framework, comes into play. Ionic is designed to fill the UI gap in hybrid mobile apps, building on the familiar AngularJS MVC tooling with new interaction models and CSS styling.

You can build the core of your code in Cordova, then use Ionic to build the appโ€™s UI. Iconic is a popular tool that shows up in the top 40 GitHub projects, with more than 500,000 applications using the framework. Itโ€™s designed to build mobile apps that bridge native and Web technologies โ€” donโ€™t try to use it for a Web app, not even a mobile Web app.

Spinning up Ionic

Getting started with Ionic is easy: Use Node.js to download and install the Ionic and Cordova tooling.

Once thatโ€™s in place, start with one of Ionicโ€™s templates. If youโ€™re building an Ionic app on a Windows development machine, youโ€™ll find the templates in the Visual Studio gallery. The three basic templates are a blank app, a tabbed app with a header, and a side menu (using the much maligned hamburger control).

You can then add your own content and code to your chosen template. The Ionic command line lets you choose the platforms youโ€™re building for and includes build tools along with an emulator that lets you quickly test your code.

The heart of Ionic lies in its CSS components. While you can customize them, the defaults are more than adequate to build prototypes or departmental apps with a very iOS look and feel. Key elements include headers available in a range of colors โ€” each with their own name, so you can use conditional CSS to change colors as required. For example, you can use a red header to indicate an alert, with blue for normal conditions.

Ionicโ€™s headers and footers are fixed, while the main content block is scrollable, so you can display large amounts of data. Because Ionic builds on AngularJS, you can use it to host a single-page application, simplifying layout and reducing the amount of content that needs to be downloaded with each interaction.

Icons, buttons, and fonts

Buttons are an interesting case. While you can use your own graphics, Ionic includes a set of its own icons delivered as a font. Itโ€™s an approach that simplifies header and menu design.

You can use standard blocks of colored text as buttons as well, with CSS definitions for finger-friendly buttons that can be variable width โ€” as wide as the parent block or as wide as the screen. Thereโ€™s also the option to use outlined buttons with colored text.

If youโ€™ve used Microsoftโ€™s modern design language, youโ€™ll find Ionicโ€™s icon font familiar. Itโ€™s a selection of simple, clear icons that handle most app use cases, with more than 500 icons for everything from email to cloud sync. You can build icons into buttons or embed them in header and footer buttons for commonly used tasks.

One useful option for header icon buttons is to use the clear option, so you have only icons or text in your header. Using a font can also reduce the size of an app, because you no longer have to pack various-size icon images for different screens. The fonts simply render appropriately on screens of different sizes.

Ionic provides CSS classes to handle layouts for most common UI models. Youโ€™ll find tools for handling lists with the option of including buttons and icons alongside text and images. If youโ€™re building an Android app, you can also use cards to display larger amounts of information. Cards can have lists and images embedded, so theyโ€™re useful UI elements in their own right.

Making it real

After youโ€™ve chosen your Ionic UI elements, you can use AngularJS (or other JavaScript) code to hook them up to dynamic content. Static lists make good navigation elements, but in practice much of your mobile app will drive Ionic programmatically.

Ionicโ€™s CSS-based approachย makes a lot of sense, because you can simply wrap your dynamic JavaScript code in Ionic CSS. If you prefer to use a grid, Ionic uses flexboxes to build complex row and column-based interfaces, including responsive grids for UIs that will work on different-size screens.

Forms are important for any HTML-based app, and Ionic makes it easy to give your forms a native look and feel. If youโ€™re using HTML5 forms, Ionic will even work with the appropriate keyboard, launching a numeric one when necessary. Forms can even be embedded in headers, giving you a constant search box, for example. There are also interesting adaptations for touch UIs, with check boxes replaced by a sliding toggle switch and a range input manifested in the form of a simple slider.

More complex UI elements are available as JavaScript modules that can be called from AngularJS code. Whereas CSS elements are relatively static, these modules can be animated โ€” for switching between hidden and visible states,for example, or allowing you to open up a slide-out settings pane or a pop-up action. Other options include support for a range of touch actions and gestures, so you can trigger different actions with long- and short-presses or with swipes.

Ionic goes a long way to deliver a native look and feel to hybrid apps. If youโ€™re using Cordova, all you need are references to Ionicโ€™s CSS and JavaScript files. Itโ€™s a good idea to build a UI using Ionicโ€™s templates before merging it with Cordova code โ€” and to ensure your AngularJS code works as a stand-alone Web app before using Ionic to style it and add app-specific UI elements. Youโ€™ll be pleased with the end result.

Simon Bisson

Author of InfoWorld's Enterprise Microsoft blog, Simon Bisson prefers to think of โ€œcareerโ€ as a verb rather than a noun, having worked in academic and telecoms research, as well as having been the CTO of a startup, running the technical side of UK Online (the first national ISP with content as well as connections), before moving into consultancy and technology strategy. Heโ€™s built plenty of large-scale web applications, designed architectures for multi-terabyte online image stores, implemented B2B information hubs, and come up with next generation mobile network architectures and knowledge management solutions. In between doing all that, heโ€™s been a freelance journalist since the early days of the web and writes about everything from enterprise architecture down to gadgets. He is the author of Azure AI Services at Scale for Cloud, Mobile, and Edge: Building Intelligent Apps with Azure Cognitive Services and Machine Learning.

More from this author