Android Wear Hello World

Filed Under: Android

Today, we’ll be discussing Android Wear with a basic hello world application using Android Studio and a Wear OS Emulator.

Android Wear App Types

Android Wear Apps can be of two types:

  • Standalone App
  • Companion App – this has an equivalent phone app which can communicate with the wear app.

Let’s start by creating a Simple Standalone Wear OS App in our Android Studio Project.

Getting Started with Android Wear Hello World App

Choose Wear OS project template from the wizard as shown below:

Android Wear Hello World Getting Started 1

Android Wear Hello World Getting Started 1

Android Wear Hello World Getting Started 2

Android Wear Hello World Getting Started 2

Project Structure

This how our project structure looks like:

Android Wear Hello World Project Structure

Android Wear Hello World Project Structure

The dependencies for wear in the build.gradle are:


dependencies {
    implementation 'com.google.android.support:wearable:2.4.0'
    implementation 'com.google.android.gms:play-services-wearable:16.0.1'
    implementation 'com.android.support:percent:28.0.0'
    implementation 'com.android.support:support-v4:28.0.0'
    implementation 'com.android.support:recyclerview-v7:28.0.0'
    implementation 'com.android.support:wear:28.0.0'
    compileOnly 'com.google.android.wearable:wearable:2.4.0'
}

AndroidManifest.xml

The Wear OS App’s manifest is slightly different from the normal Android Phone Apps.
Here, we need to specify the feature and wear OS library along with some metadata.


<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.journaldev.androidwearoshelloworld">

    <uses-permission android:name="android.permission.WAKE_LOCK" />

    <uses-feature android:name="android.hardware.type.watch" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@android:style/Theme.DeviceDefault">
        <uses-library
            android:name="com.google.android.wearable"
            android:required="true" />

        <!--
               Set to true if your app is Standalone, that is, it does not require the handheld app to run.
        -->
        <meta-data
            android:name="com.google.android.wearable.standalone"
            android:value="true" />

        <activity
            android:name=".MainActivity"
            android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

Layout

The most common layout used for smartwatches having wear os is BoxInsetLayout.


<android.support.wearable.view.BoxInsetLayout>
   ...
   <LinearLayout
      ...
      app:layout_box="all">
</android.support.wearable.view.BoxInsetLayout>

Another layout commonly used is SwipeDismissFrameLayout. This enables swipe from left to right.

The RecyclerView equivalent class for wear os is WearableRecyclerView.

android.support.wearable.view.CircledImageView provides a circular layout to display the image.

The code for the activity_main.xml layout is given below:


<?xml version="1.0" encoding="utf-8"?>
<android.support.wear.widget.BoxInsetLayout
    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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/dark_grey"
    android:padding="@dimen/box_inset_layout_padding"
    tools:context=".MainActivity"
    tools:deviceIds="wear">



    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:padding="@dimen/inner_frame_layout_padding"
        app:boxedEdges="none">


        <android.support.wear.widget.SwipeDismissFrameLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/swipe_dismiss_root" >

            <TextView
                android:id="@+id/test_content"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:gravity="bottom"
                android:text="Swipe the screen to dismiss me." />
        </android.support.wear.widget.SwipeDismissFrameLayout>


        <TextView
            android:id="@+id/text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:text="@string/hello_world" />

    </FrameLayout>
</android.support.wear.widget.BoxInsetLayout>

Here we’ve added a swipe to dismiss layout with a text view in it.

Code

The code for the MainActivity.java class is given below:


package com.journaldev.androidwearoshelloworld;

import android.os.Bundle;
import android.support.wearable.activity.WearableActivity;
import android.widget.TextView;

public class MainActivity extends WearableActivity {

    private TextView mTextView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mTextView = findViewById(R.id.text);

        // Enables Always-on
        setAmbientEnabled();
    }
}

The always-on feature allows the app to control what to show on the watch while it’s in ambient mode.

Let’s look at our Hello World Wear OS Application when run on the emulator:

Android Wear Os Hello World Output

Android Wear Os Hello World Output

That brings an end to this Hello World Wear OS Android Tutorial.
You can download the project from the link below or view the full source code in the Github Repository.

Leave a Reply

Your email address will not be published. Required fields are marked *

close
Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages