Studying new programming language always begins with "Hello World" program. We are going to do the same and we will create an application for handheld devices that will send "Hello World" notification to an Android Wear Device.

In my previous tutorials you can read how to set up development environment, create Android Wear emulator and connect it with your handheld device:

www.smartwatch.me [TUTORIAL] How to develop Android Wear apps for Beginners. Part 1 - Setup Android Wear is a new version of the Android system designed specifically for wearables , which was announced by Google in march 2014. Currently lots of devices running on Android Wear were released, including well designed Moto 360 and Gear Live by Samsung.But still, amount of applications using all the benefits of Android Wear in Google Play is considerably small. In this tutorial I'm going to show you how to create apps for Android Wear from Scratch. Set up the Android development en...

www.smartwatch.me [TUTORIAL] How to develop Android Wear apps for Beginners. Part 2 - Connect Android Wear with handheld device In my previous tutorial I told you how to set your development environment for Android Wear, create and run a project from Google samples, and how to create Android Wear (AVD). You can read it here: You can develop not only Wearable apps with Android Wear SDK, but also you can create apps for smartphones and tablets (we are going to refer to them as handheld devices) that support output of data on Android Wear. We are going to create this kind of application in next tutorial. In this tutorial...

Firstly, we are going to look at the program structure in Android. Android applications usually consist of the set of Activities.



What is Activity?

An activity represents a single screen with a user interface.

For example: an email application might have one activity that shows a list of new emails, another activity to compose an email. An application usually consists of multiple activities that are loosely bound to each other.

Typically, one activity in an application is specified as the "main" activity, which is presented to the user when launching the application for the first time. Each activity can then start another activity in order to perform different actions.

Our application will have two main activities:

One for the handheld device(HandHeldActivity.java) One for the wearable(WearActivity.java)

To create notification, we will add code in the handheld activity only, located in the “mobile” module of the project.

Please notice:

If you want to know more about Activity and its life cycle, you can visit official Google documentation page about Activity.

I also recommend you to read this tutorial about Android Structure, if words "layout", "fragment" and "manifest" don't ring a bell.

Now let's create a project

Creating project

Run Android Studio



Select Start a New Android Studio Project in the start screen or if you are in another project select File>New Project.



In the new project dialog, enter Application Name as Simple Notification, Domain as smarthwatches.me, and a project directory (you can store your project wherever you want). Click Next.





In the form factors dialog, choose Phone and Tablet and Wear. Keep SDKs options by default, and then click Next.



In the mobile activity dialog, choose Blank Activity. Click Next.



In the mobile option dialog enter an Activity Name for the handheld as HandHeldActivity. Keep other fields by default and click Next.





In the wear activity dialog, select Blank Wear Activity. Then click Next.



In the wear options dialog enter an Activity Name for the wearable as WearActivity. Keep other fields by defaults and click Finish.



Application Structure

When our application is run on a handheld device, it will show screen with one button - "Show Notification". If you push this button, notification "Hello World" will be sent to your watch. That's all, it's very simple app.

This is the screen of our future app:



What are notifications in Android?

Android allows you to put notification into the titlebar of your application. The user can expand the notification bar and by selecting the notification the user can trigger another activity.

The easiest way is to create a usual handheld device Notification in Android:

Create an object of Notification сlass, using the Notification.Builder class, which provides an builder interface, where you can set different parameters(like your custom message) Send it using the NotificationManager class , which can be received from the Context (from Activity or a service, using getSystemService() method). These notifications should be displayed on both the handheld device and wearable emulator with a similar level of detail.

Please notice:

If you want to know more about Notification visit official Google documentation page about Notification.

Although Android supports pulling notifications from paired handheld device, and displaying them as cards in it's stream, if your app doesn't explicitly support Android Wear, there's no guarantee its notifications will display correctly on an Android Wear device. If you want to be sure that your notification will appear on your Android Wear, you need to create handheld notifications that can seamlessly extend to a paired wearable device

Only Android with 4.4 version or higher has Android Wear SDK (about 30 percent of all devices on Android have Android version 4.4 or higher). In order to support connection between Android Wear and handheld devices with older Android version we are going to use compatible versions like NotificationCompat.Builder and NotificationManagerCompat .

If you want to use these classes in your app, you need to add Support Library to your app. To do this:

Go to Gradle Scripts > build.gradle(module: mobile)



Check that in block «dependencies» there is this line:

compile 'com.android.support:appcompat-v7:21.0.3' or the newest version of support library at the moment.

Coding part

First step

Open layout, connected with HandHeldActivity.java , you can find it in mobile>res>layout it's called activity_hand_held.xml .

Please notice:

If you don't know what layout is, read the tutorial that I recommended in the beginning of this tutorial.

Basically, layout defines the visual structure for a user interface, such as the UI for an activity or app widget. Layout consists of objects of View class. This class represents the basic building block for user interface components. View is the base class for widgets, which are used to create interactive UI components (buttons, text fields, etc.). You can set properties for Views, set up listeners for them, which will be notified when something interesting happens to the view(for example, when someone clicks the button). You can declare a layout in two ways:

Declare UI elements in XML. Android provides a straightforward XML vocabulary of Views classes(such as TextView, Button, ListView or e.t.c.) or their subclasses.

Instantiate layout elements at runtime. Your application can create View and ViewGroup objects from Java programming language.

One of really cool benefits of Android Studio is an advanced layout editor that allows you to drag-and-drop widgets into layout and preview it editing the XML.In the layout editor, you can switch between the Text view, where you edit the XML file as text, and the Design view.

Second step

Open layout editor in Design view . Add a button from category Widgets. You can edit its text by double clicking on it or via attribute text in xml. Go to Text view and change button's attribute android:layout_width to fill_parent . Pay attention to button's attribute android:id , it should be '@+id/button'. The plus sign simply indicates that the id should be created if it doesn't exist. You can use R.id.button to reference this button from code.

Your xml file should look like mine:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".HandHeldActivity"> <Button android:id="@+id/button" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" android:layout_alignParentTop="true" android:text="Show notification" /> </RelativeLayout>

If you build and run your project on a handheld device, you'll see a screen with one button. But nothing will happen if you push this button. ClickListener is a class responsible for button reaction. We are going to appoint action(trigger notification in our case) with a button.



Third Step

Open HandHeldActivity.java , you can find it in

moble>java>me.smarthwatches.simplenotification .

Import all packages you need for triggering notification(notification package, button package, notification compatible packages):

import android.support.v4.app.NotificationManagerCompat;

import android.support.v4.app.NotificationCompat;

import android.app.Notification;

import android.widget.Button;

All remaining code we will add into onCreate(Bundle) method of this Activity.



Please notice:

onCreate(Bundle) is method, where you initialize your activity.It is called everytime you start the app. Most importantly, here you will usually call setContentView(int) with a layout resource defining your UI (in our case we will call setContentView(R.layout.activity_hand_held) ) and using findViewById(int) to retrieve the widgets in that UI that you need to interact with programmatically(using this method we will retrive our button)

Declare a variable mButton (By Android code style name of variable starts with "m") and retrieve button from UI using findViewById(R.id.button) method. This method returns a usual View object, so we need to cast it to Button class. Button mButton = (Button) findViewById(R.id.button); Now we need to set onClickListener for our mButton .

For this we use one of the most useful features of Android Studio - smart code completion. Type mButton and put an dot after it.Android Studio will suggest you possible completion, you can type 'set' and studio will suggest only methods or variables, starting with or containing 'set' as substring. Choose setOnClickListener(OnClickListener l) .



We need onClickListener as a parameter in brackets, which is able to trigger notification, when button is clicked. For this type "new" (with this keyword we create a new object) and enter hot-key Ctrl+Space , Android Studio will suggest your possible objects, you can pass into this method. Choose create a custom OnClickListener class, as shown on screenshot:



Android Studio will create custom class OnClickListener with empty method onClick(View v) , which is called when button is clicked.



All remaining code we will put in this method. Create a normal Android notification using the NotificationCompat.Builder class and set desired properties Notification notification = new NotificationCompat.Builder(getApplication())

.setSmallIcon(R.drawable.ic_launcher)

.setContentTitle("Hello World")

.setContentText("My first Android Wear notification")

.extend(

new NotificationCompat.WearableExtender().setHintShowBackgroundOnly(true))

.build(); Get an instance of the NotificationManagerCompat service. NotificationManagerCompat notificationManager = NotificationManagerCompat.from(getApplication()); Give notification id and display it. int notificationId = 1;

notificationManager.notify(notificationId, notification);

Build and run this app. If your click button "Show notification", it will trigger notification with a title "Hello World" and body "My first Android Wear notification". If you can't see this notification cause of others, scroll through other notifications to view this one.







You can get code of all project from Github:

https://github.com/calmarj/android-wear-hello-world



Sum up

In this tutorial we created our first real for handheld device, which supports notification on Android Wear device. Also we learned what is Activity, layout and e.t.c. In next tutorial we will create full-screenWearable app. Please don't hesitate to ask any questions and leave feedback. Thank you for reading.