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:
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.
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
- 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.
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.
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
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.
Open layout, connected with
HandHeldActivity.java, you can find it in
mobile>res>layout it's called
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.
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
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:
android:text="Show notification" />
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.
HandHeldActivity.java, you can find it in
Import all packages you need for triggering notification(notification package, button package, notification compatible packages):
All remaining code we will add into
onCreate(Bundle) method of this Activity.
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 mButton = (Button) findViewById(R.id.button);
Now we need to set
onClickListener for our
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
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())
.setContentText("My first Android Wear notification")
Get an instance of the
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(getApplication());
Give notification id and display it.
int notificationId = 1;
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:
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-screen
Wearable app. Please don't hesitate to ask any questions and leave feedback. Thank you for reading.