Android Wear is somewhat different from apps running on phones or tablets. Instead of icons and lists, Android Wear uses cards, which is something we saw already in the recipes that introduced us to the basic concepts of material design.
According to the context and only at a relevant moment, a card is added to the stream of cards once a new notification arrives. This is known as the context stream, and it does contain various interesting pieces of information. Think of them as incoming emails, the weather, the number of steps you took today, your heart beat rate, and other events or reminders.
Remember the water app from the previous chapter? For example, we could create a notification to remind us to drink water more often and to add a new card for it. This would be a nice feature to have.
This recipe requires Android Studio and the latest SDKs, including the wear SDK, installed. Check out the previous recipe for more information.
You also need a handheld device running on Android Lollipop or above that has the Android Wear
app installed and a wearable device that is connected to your handheld device through Bluetooth.
Let's see how notifications can be triggered and how to display them nicely on a smartwatch:
WaterNowNotification
and click on the Next button.WaterNowActivity
and click on the Finish button.build.gradle
file in your app. Add this to the dependencies section and apply the appropriate version for it:compile 'com.android.support:support-v4:22.0+'
activity_water_now.xml
file and change it to the Text mode using the tab at the bottom of Android Studio.<LinearLayout 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:orientation="vertical" tools:context=".WaterNowActivity"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Drink water now!" android:id="@+id/water_now_button" android:layout_gravity="center" /> </LinearLayout>
onCreate
method of the WaterNowActivity
class, add an onClick
handler for the button that we just created. Use the Alt + Enter shortcut to add import statements as needed:Button waterNowButton = (Button)findViewById(R.id.water_now_button); waterNowButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { sendNotification(); } });
sendNotification
method:private void sendNotification(){ NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder( WaterNowActivity.this) .setContentTitle("Water app!") .setSmallIcon(R.drawable.icon) .setContentText("Hey there! Drink water now!"); NotificationManagerCompat notificationManager =NotificationManagerCompat.from( WaterNowActivity.this); notificationManager.notify(1 , notificationBuilder.build()); }
res/drawable
folder. Create a drawable icon.xml
file and add the implementation to create a nice blue circle:<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android= "http://schemas.android.com/apk/res/android"android:shape="oval"> <corners android:radius="10dip"/> <stroke android:color="#0000FF" android:width="15dip"/> <solid android:color="#0000FF"/> </shape>
Android wear
app to check this) and run the app. You will see the output similar to the following screenshot:For other Android versions or for particular brands (customized Android versions), the setting you are looking for may be located elsewhere and/or may have a different name.
Where to go from here? You can combine this notification recipe with the Water app from the recipes within the Chapter 3, Material Design and create something cooler or you can check whether you can find a way to customize the notification.
Smartwatches, phones, phablets, and tablets come with screens of all sizes and shapes. How can we benefit from a larger screen or how can we provide smart navigations for smaller screens and maintain the same functionality and code within one app?
Different layouts for different Android versions? Multiple layouts are what we need combined with multiple fragments. This is where the recipes in the next chapter come in.