Search icon
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Android Wear Projects

You're reading from  Android Wear Projects

Product type Book
Published in Jul 2017
Publisher Packt
ISBN-13 9781787123229
Pages 416 pages
Edition 1st Edition
Languages
Author (1):
Ashok Kumar S Ashok Kumar S
Profile icon Ashok Kumar S

Table of Contents (18) Chapters

Title Page
Credits
About the Author
About the Reviewers
www.PacktPub.com
Customer Feedback
Preface
Getting You Ready to Fly - Setting Up Your Development Environment Let us Help Capture What is on Your Mind - WearRecyclerView and More Let us Help Capture What is on Your Mind - Saving Data and Customizing the UI Measure Your Wellness - Sensors Measuring Your Wellness and Syncing Collected Sensor Data Ways to Get Around Anywhere - WearMap and the GoogleAPIclient Ways to Get Around Anywhere - UI controls and More Let us Chat in a Smart Way - Messaging API and More Let us Chat in a Smart Way - Notifications and More Just a Face for Your Time - WatchFace and Services More About Wear 2.0

Chapter 1. Getting You Ready to Fly - Setting Up Your Development Environment

The culture of Wearing a utility that helps us to perform certain actions has always been part of a modern civilization. Wrist watches for human beings have become an augmented tool for checking the time and date. Wearing a watch lets you check the time with just a glance. Technology has taken this watch-wearing experience to the next level. The first modern Wearable watch was a combination of a calculator and a watch, introduced to the world in 1970. Over the decades, advancements in microprocessors and wireless technology have led to the introduction of a concept called ubiquitous computing. During this time, most leading electronics industry start-ups started to work on their ideas, which has made Wearable devices very popular.

Tech giant companies, such as Google, Apple, Samsung, and Sony, have joined the force of the Wearable devices era. They have introduced their competitive Wearable products, which are extremely successful in the Wearable device market. More interestingly, Google's Android Wear is powerful, follows the same Android smartphone development practices, and has a very good developer community compared to Apple Watch OS and Samsung's Tizen OS developer community.

Google announced Android Wear in March 2014. Since then, Android Wear as a smartwatch and Wearable software platform has evolved. Google's continuous advancement in designing and user experience have resulted in a new generation of the Android Wear operating system, which has the ability to handle biometric sensors like never before with more features in the platform; Google calls it Android Wear 2.0.

Android Wear 2.0 will cause a lot of excitement in app development with remarkably competitive features to develop. Android Wear 2.0 allows a developer to build and carve his idea specific to Android Wear; there is no need to pair a watch and mobile app. Google calls it a standalone application. Android Wear 2.0 introduces a new way to input within the Android watch: a new application programming interface called Complications, which allows watch faces to display vital information from biometrics and other sensors. New updated notifications support for Android Wear 2.0 will help users and developers to present notifications in a more comprehensive manner.

In this chapter, we will explore the following:

  • Android Wear design principles
  • Exploring essential UI components specific to Wear apps
  • Setting up a development environment for Wear apps development
  • Creating your first Android Wear application

Android Wear design principles


Designing a Wear is different than a mobile or tablet application. The Wear operating system is very lightweight and has a specific set of jobs to accomplish by sharing the right information with the Wearer.

General Wear principles are Timely, Glanceable, Easy to Tap, Time-Saving.

Timely

Giving the right information at the right time.

Glanceable

Keeping the Wear application user interface clean and uncluttered.

Easy to Tap

The actions users will click on should have the right spacing and size of the picture.

Time-Saving

Creating the best application flows that do tasks quickly.

For any Wear application, we need the proper building blocks to control the business logic of the application and other architectural implementation. The following are the scenarios for developing a Wear application to help us to carve the wear application better:

  • Defining layouts
  • Creating lists
  • Showing confirmations
  • Wear navigation and actions
  • Multifunction buttons

Defining layouts

Wearable can use the same layouts that we use in handheld Android device programming but with specific constraints for Wear applications. We should not do heavy processing actions similar to handheld Android devices in Wear applications and expect a good user experience.

An application designed for a round screen will not look great on square Wear devices. To resolve this, the Android Wear support library comes with the following two solutions:

  • BoxInsetLayout
  • Curved Layout

We can provide different resources to allow Android to detect the shape of the Android Wear at runtime.

Creating lists

Lists let the user an item from a set of items. In the legacy Wear, 1.x API WearableListView helped programmers to build lists and custom lists. Wearable UI library now has WearableRecyclerView with curvedLayout support and has the best implementation experience in Wear devices.

We can add gestures and other magnificent functionalities:

Exploring UI components for Wear devices


In this subchapter, let's explore the used Wear-specific UI components. In Wear application programming, we can use all the components that we use in mobile app programming, but how we accommodate the visual appearance of components in the Wear device needs to be well thought of before using it.

WatchViewStub: WatchViewStub helps in rendering the views for different form factors of Wearable devices. If your application is being installed on a round watch device, WatchViewStub will load the specific layout configuration for round watches. If it is square, it will load the square layout configuration:

<?xml version="1.0" encoding="utf-8"?>
<android.support.wearable.view.WatchViewStub xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/watch_view_stub"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:rectLayout="@layout/rect_activity_main"
    app:roundLayout="@layout/round_activity_main"
    tools:context="com.ashokslsk.wearapp.MainActivity"
    tools:deviceIds="wear"></android.support.wearable.view.WatchViewStub>

WearableRecyclerView: WearableRecyclerView is the implementation of recyclerview specific to wearable devices. It provides a flexible view for datasets in the Wearable device viewport. We will explore WearbaleRecyclerView in detail in the coming chapters:

 <android.support.wearable.view.WearableRecyclerView
   android:id="@+id/recycler_launcher_view"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:scrollbars="vertical" /> 

Note

Note:WearableListView is deprecated; the Android community recommends using WearableRecyclerView.

CircledImageVIew: An Imageview surrounded by a circle. A very handy component for presenting the image in round form factor Wearable devices:

<?xml version="1.0" encoding="utf-8"?>
<android.support.wearable.view.CircledImageView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/circledimageview"
    app:circle_color="#2878ff"
    app:circle_radius="50dp"
    app:circle_radius_pressed="50dp"
    app:circle_border_width="5dip"
    app:circle_border_color="#26ce61"
    android:layout_marginTop="15dp"
    android:src="@drawable/skholinguaicon"
    android:layout_gravity="center_horizontal"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

BoxInsetLayout: This Layout directly to Framelayout and it has the ability to recognize the form factor of the Wearable device. Shape-aware FrameLayout can box its children in the center square of the screen:

<android.support.wearable.view.BoxInsetLayout    xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.ranjan.androidwearuicomponents.BoxInsetLayoutDemo">

<TextView
    android:text="@string/hello_world"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:layout_box="all" />

</android.support.wearable.view.BoxInsetLayout>

After the Wear 2.0 release, a few components were deprecated for an immersive activity experience and Google strictly prohibits using them; we can still use all the components that we know in Android programming.

Showing confirmations

Compared to in handheld Android devices, in Wear applications, confirmations should occupy the whole screen or more than what handheld devices show as a dialogue box. This ensures users can see these confirmations at one glance. The Wearable UI library helps in displaying confirmation timers and animated timers in Android Wear.

DelayedConfirmationView

A DelayedConfirmationView is an confirmation view based on the timer:

<android.support.wearable.view.DelayedConfirmationView
android:id="@+id/delayed_confirm"
android:layout_width="40dp"
android:layout_height="40dp"
android:src="@drawable/cancel_circle"
app:circle_border_color="@color/lightblue"
app:circle_border_width="4dp"
app:circle_radius="16dp">
</android.support.wearable.view.DelayedConfirmationView>

Wear navigation and actions

In the new of Android Wear, the Material design library the following two interactive drawers:

  • Navigation drawer
  • Action drawer

Navigation drawer

Lets user switch between in the application. Developers can allow the drawer to be opened anywhere within the scrolling parent's content by setting the setShouldOnlyOpenWhenAtTop() method to false:

<android.support.wearable.view.drawer.WearableNavigationDrawer
android:id="@+id/top_drawer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/holo_red_light"
app:navigation_style="single_page"/>

Action drawer

The action drawer gives to easy and common actions in your application. By default, action drawer appears at the bottom of the screen and provides specific actions to users:

<android.support.wearable.view.drawer.WearableActionDrawer
android:id="@+id/bottom_drawer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/holo_blue_dark"
app:show_overflow_in_peek="true"/>

Multifunction buttons

In addition to the power button, Android Wear supports another button called the multifunction button on the device. The Wearable library provides API for determining the multifunction buttons included by the manufacturer:

@Override
// Activity
publicboolean onKeyDown(int keyCode,KeyEventevent){
if(event.getRepeatCount()==0){
if(keyCode ==KeyEvent.KEYCODE_STEM_1){
// Do stuff
returntrue;
}elseif(keyCode ==KeyEvent.KEYCODE_STEM_2){
// Do stuff
returntrue;
}elseif(keyCode ==KeyEvent.KEYCODE_STEM_3){
// Do stuff
returntrue;
}
}
returnsuper.onKeyDown(keyCode,event);
}

Visit https://developer.android.com/training/wearables/ui/index.html for any sort of query that you might have on design guidelines for Wear device programming.

Setting up a development environment for Wear development


In this section, we set up a development environment for Wear development.

Prerequisites

  1. Your favorite operating system (Windows, macOS, or Linux)
  2. Determine whether you have the latest JRE installed on your operating system
  3. Install the latest version of JDK or Open JDK
  4. Install the latest version of Android Studio (at the time of writing this book, the latest version is 2.2.3 and any newer version should be fine)

Installing Android Studio

Visit https://developer.android.com/studio/index.html to download the latest version of Android Studio. Google highly recommends using Android Studio for all Android application development, since Android Studio has tight with and useful Android APIs:

After the Android Studio installation, it's now time to download the necessary SDK in the SDK Platforms tab in SDK Manager. Install one complete version of Android; for the scope of this book, we will install Android 7.1.1 API level 25:

After the successful installation of the SDK of Nougat 7.1.1 API level 25, under the SDK Tools tab, make sure you have the components, as shown in the following screenshot:

  • Android Support Library
  • Google Play services
  • Google Repository
  • Android Support Repository

Google releases updates on IDE and SDK Tools frequently; keep your development environment up-to-date.

Note

Note: if you plan to make your application available in China, then you must use the special release version 7.8.87 of the Google Play services client library to handle communication between a phone and watch: https://developer.android.com/training/wearables/apps/creating-app-china.html

Visit the following link to check the Release Notes on SDK Tools: https://developer.android.com/studio/releases/sdk-tools.html.

Updating your IDE from the stable channel is highly recommended. Updates for Android Studio are available on four different channels:

  • Canary channel
  • Dev channel
  • Beta channel
  • Stable channel

Canary channel: The Android Studio team works continuously to make Android Studio better. In this channel, every week there will be an update release, and it will include new functionality changes and improvements; you can check those changes in the release notes. But updates from this channel are not recommended for application production.

Dev Channel: On this channel, a happens after a complete round of internal testing from the Android Studio team.

Beta channel: On this channel, updates are based on stable Canary builds. Before publishing these builds to a stable channel, Google releases them in the beta channel to get developer feedback.

Stable Channel: Are the official stable releases of the Android Studio and will be available to download on Google's page http://developer.android.com/studio.

By default, Android Studio receives from a stable channel.

Creating your first Android Wear application


In this section, let's understand the steps required to create your first Wear project.

Note

Before you continue to create your application, ensure you have one complete version of Android installed with a Wear system image and you have the latest version of Android Studio.

The following picture is the initial interface of Android Studio. In this window, one can import legacy ADT Android projects, configure the Android SDK, and update Android Studio.

Android Studio welcome window with basic controls for getting started:

Creating your first Wear project

Click on the Start a new Android Studio project option in the Android Studio window. You will be prompted by another with project details.

The following screenshot shows the window that allows users to configure their project details, such as project name, Package name, and whether the project needs native C++ support:

You can name your project as you wish. After you have chosen your project name and your project local system location, you can press the Next button in the window, which brings up another window with a few queries, as shown in the following screenshot:

In this window, you can choose to write a standalone Wear application if you uncheck the Phone and Tablet option. In way, you will see only Wear application templates:

Now, Android Studio templates prompt only Android Wear activity templates with the following set of options:

  • Add No Activity
  • Always On Wear Activity
  • Blank Wear Activity
  • Display Notification
  • Google Maps Wear Activity
  • Watch Face

The activity template helps you to access the default boilerplate codes, which are already templatized and can be used directly in projects:

To create the first project, we will choose Blank Wear Activity and click on the Next button in the window. Android Studio will prompt another window for creating the name of the activity and layout file. In this template, the two form factors of Android Wearable devices, which are mostly round and square shapes, are with the boilerplate code stub:

When your project is ready to be created, click on the Finish button. After clicking on Finish, Android Studio will take a few moments to create the project for us.

Way to go! You have now created a working boilerplate code for the Android Wear standalone application without the phone companion application. When successfully created, you will see the following files and codes to your project by default:

If your SDK is not updated with API level 25, you might see the Wear option in the Android Studio project creating prompts with Android Wear support library 1.x; you can update this in the Wear module Gradle file with the following dependency:

compile 'com.google.android.support:wearable:2.0.0'

Creating a Wear emulator

The process of a Wear emulator is very to creating a phone emulator.

In the AVD manager, click on the Create Virtual Device... button:

Choose the required form factor emulator according to your application needs. Now, let's create the Android Wear square emulator:

After selecting the right emulator for your Wear, you will get another prompt to choose the Wear operating system. Let's the API Level 25Nougat emulator, as in the following screenshot:

The last prompt asks for the emulator name and other orientation configurations based on your needs:

Way to go! Now, we have successfully created a square form emulator for the project. Let's run the project that we have in the emulator:

Google recommends developing Wear apps in the actual hardware device to have the best user experience. However, working on emulators has the benefit of creating different screen form factors to check the application's rendering.

Working with actual Wear device
  1. Open the settings on the Wear device
  2. Go to About device
  3. Click on the build number seven times to enable developer mode
  4. Now enable ADB debugging on the watch

You can now connect the Wear device directly to your machine with the USB cable. You can debug your applications over Wi-Fi and Bluetooth with the following setups.

Debugging over Wi-Fi

Make sure your watch has the options enabled. Debugging over Wi-Fi is possible only when the Wear device and machine are connected to the same network.

  • In the Wear device developer option, tap on Debug over Wi-Fi
  • The watch will display its IP address (for example, 192.168.1.100). Keep a reference; we need this for the next step.
  • Connect the debugger to the device
  • Using the following command, we can attach the actual device to the ADB debugger:
adb connect 192.168.1.100
Enable Bluetooth debugging

We need to ensure debugging is in developer options, as follows:

adb forward tcp:4444 localabstract:/adb-hub
adb connect 127.0.0.1:4444

In your Android Wear, just allow ADB Debugging when it asks.

Now that we have a working setup of our development environment, let's understand the basic Android Wear-specific UI components.

Summary


In this chapter, we have looked at the initial setup for Wear application development. We have understood the necessary components to download, setting up a Wear emulator, connecting the Wear emulator to the ADB bridge, debugging over Wi-Fi, and essential user interface components specific to Wear development. In the next chapter, we will explore how to build a note-taking application which persists the data that users enter.

 

You have been reading a chapter from
Android Wear Projects
Published in: Jul 2017 Publisher: Packt ISBN-13: 9781787123229
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at £13.99/month. Cancel anytime}