This is a continuation from our previous blog The Wearable Computing Revolution Begins: Building Android Wear App where we discussed Android Wear. In this blog, we will discuss the code involved.
There are few things that we need to keep in mind before starting our project. Here they are:
- Make sure you are using the latest release of Android Studio.
2. Install latest Android SDK Platform as well as Android Wear System Image as shown in the image below:
Your system is now ready to create your first Android Wear application.
Getting Started
Steps:
- We’ll now start our new project – Create New Project.
- Select the form factors – “Phone and Tablet” and “Wear” as shown below:
- Go to next and finish the template
- Add blank activity.
- Give the project name as “Wear Demo”.
Now let’s discuss about the following three files that will be required:
- MainActivity.java: It is the Java code that launches the app and we will be adding the code.
- activity_main.xml: It is the user interface file for the watches that use Rectangle screen.
- strings.xml: This is be used to store our string values in order to localize our app.
Creating our UI (User Interface)
Let’s start by creating UI for Square screen watch.
- Click on activity_main.xml and you will see the TextView.
- We can change the Text Value in string.xml file as “Hello AcadGild!”.
- Drag a small button widget and name it as “CLICK IT”.
- Drag it into center of the view.
- It will be displayed as shown in the picture below:
Code:
- Go to MainActivity.java and add the following code for:
Declare Button and TextView as follow:
1 2 3 |
Button btnClick; TextView txtMessage; |
Initialize Button and TextView as:
1 2 3 |
btnClick= (Button) findViewById(R.id.button); txtMessage= (TextView) findViewById(R.id.textMessage); |
2. Create Button’s onClick() and put the code for changing the TextView color when Button is clicked. Follow the code given below:
1 2 3 4 5 6 7 8 9 10 11 |
btnClick.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { txtMessage.setTextColor(Color.RED); } }); |
Note: Make sure that there are no errors in the code. It’s time now to run your app.
Test your app
1. Click on the Run button to run your app as shown below:
2. Select the emulator as shown below:
3. Run your app and the output is as shown below:
Before clicking on Button:
After Clicking on Button:
As explained above we have now successfully created our own application. Hope this blog was informative enough and you are now ready to create your own application for Android Wear.
Leave a Reply