First Google Glass App – Part 8 – Hello Glass!

Jumping right in, let’s create a Glass GDK project from scratch:

  1. Create New Android Project
  2. Configure GDK
  3. Imports
  4. Code
  5. qwe

To create a new project…See our Part 1 of the tutorial.

Make sure to configure the Glass GDK Sneak Peek Manually if it didn’t get configured by Android Studio or Eclipse on set up.  As it turns out, even if you create a project setting GDK as the Compile for API, it doesn’t get created as such.  You must double check in your build.gradle file (CAREFUL, there are 2 such files.  You need to modify your inner most gradle file) and make sure it looks something like this:

Google Glass GDK App Build Gradle File Settings by Marcio Valenzuela Santiapps.com
Google Glass GDK App Build Gradle File Settings by Marcio Valenzuela Santiapps.com

And you need to make sure that Android 4.0.3 GDK Sneak Peek & gdk.jar got added to your External Libraries as well.

Ok, once we have that out of the way, we need to include some imports.  In this case we need to add AudioManager support because we will be working with audio.  We also need TextToSpeech for recognizing commands and KeyEvent to respond to touches.

Add the following imports:

import android.app.Activity;
import android.content.Context;
import android.media.AudioManager;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.view.View;
import android.view.KeyEvent;
import android.widget.TextView;

import com.google.android.glass.app.Card;
import com.google.android.glass.media.Sounds;

We will create the card and its view as well as add a TextView, nothing new here.  But we will create a TextToSpeech variable as well as a context.  We init our speech engine and pass it the value to be speak.

We will also be creating an onKeyDown method to respond to taps on the touchpad.  When they DO occur we will then create an AudioManager to play a tap sound, set the text view and also set speak a new value.

So lets add the following code:

public class HelloGlassActivity extends Activity {

private Card _card;
private View _cardView;
private TextView _statusTextView;

private TextToSpeech _speech;

private Context _context = this;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

// Init TextToSpeech engine
_speech = new TextToSpeech(this, new TextToSpeech.OnInitListener() {
@Override
public void onInit(int status) {
_speech.speak(“Hello Glass”, TextToSpeech.QUEUE_FLUSH, null);
}
});

// An alternative way to layout the UX
setContentView(R.layout.layout_helloworld);
_statusTextView = (TextView)findViewById(R.id.status);
}

/**
* Handle the tap from the touchpad.
*/
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
switch (keyCode) {
// Handle tap events.
case KeyEvent.KEYCODE_DPAD_CENTER:
case KeyEvent.KEYCODE_ENTER:

// Status message below the main text in the alternative UX layout
AudioManager audio = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
audio.playSoundEffect(Sounds.TAP);

_statusTextView.setText(R.string.touchpad_touched);

_speech.speak(“Touchpad touched”, TextToSpeech.QUEUE_FLUSH, null);

return true;
default:
return super.onKeyDown(keyCode, event);
}
}

@Override
public void onResume() {
super.onResume();
}

@Override
public void onPause() {
super.onPause();
}

@Override
public void onDestroy() {
super.onDestroy();
}
}

Its quite a simple app but it gets your juices flowing!

Plug in your Glass device and hit the Run button!

First Google Glass App – Part 7 – Bridge to Glass App GDK Development

Before jumping into Glass dev, let’s understand how to create a Hello World project in Android Studio (AS) and run it on our device.

  1. Create New Project
  2. Get to know the guts
  3. Add Imports
  4. Add Code
  5. Tweak guts
  6. Run on Device

Create New Project

When you select New Project from the File Menu, you get this Wizard screen:

Android Studio Beginner App Development by Marcio Valenzuela Santiapps.com
Android Studio Beginner App Development

Fill in the Application Name in a natural language and the Module Name without spaces.  Make sure to select API 15 for Minimum and Target SDK but Glass Development Kit Sneak Peek for Compile with.

Click Next and in the next screen just leave everything as is (the launch icon selector screen).

Android Studio Project Launcher Icon Window by Marcio Valenzuela Santiapps.com
Android Studio Project Launcher Icon Window

After that screen, leave the Blank Activity option selected and again click Next.

Android Studio Project Blank Activity Window by Marcio Valenzuela Santiapps.com
Android Studio Project Blank Activity Window

Finally in the Activity Name, leave MainActivity.  In the Layout Name leave activity_main but also copy that activity_main over to the Fragment Layout Name, replacing fragment_main.

Android Studio Project Layout Window by Marcio Valenzuela Santiapps.com
Android Studio Project Layout Window

Everything else stays as is and Click Finish.  The reason for this last bit is that new in Android is this concept of Fragments.  This just complicates things for us at the moment so we will leave it out for now.  We must also remove the MainActivity.java fragment method later.

Android Guts

Let’s familiarize ourselves with this screen:

Android Studio Layout Google Glass Development by Marcio Valenzuela Santiapps.com
Android Studio Layout Google Glass Development

Let’s review these 10 top pointers:

  1. Your Java Classes or Android Activities
  2. Layout files in xml format
  3. Value files for storing global settings of sorts
  4. AndroidManifest is a sort of Central Registry
  5. Top level build.gradle file
  6. Low level build.gradle file
  7. Tab bar for displayed files
  8. Sync Gradle file button
  9. Run button
  10. Green = A-Ok button!
Take some time to explore these files.  If you have gone through the tutorial at:
 
http://developer.android.com/training/basics/firstapp/index.html
 
You will be familiar with these pointers.  Otherwise, let’s take a closer look.
 
MainActivity is selected and displayed in the editor window.  This contains your default, boilerplate/template created MainActivity Class required for any app.
 
The layout folder contains your activity_main.xml file.  If you look inside that file you will see an xml file with some parameters and a TextView element.  You will also see a graphical representation of it off to the far right.
 
The values folder contains more xml files.  Most importantly, the strings.xml which contains global references to string values.  These are used throughout the app to assign string values where needed.
 
The AndroidManifest.xml file contains some general settings elements for your app such as the package name, launcher icon, activity tags which contain your declared activities and in this case, if the activity has an intent, which is like an action, it must also be specified here.  In our case we will add a Voice Trigger intent filter if there isn’t one already.
 
Build.gradle files are not to be messed around with much.  This is a new M.O. used by Android Studio to organize files in a workspace.  You must keep in mind here that there is a Top level and Low level build.gradle file so make sure you know which one you are being told to put things in.
 
The tabs display whatever files you have double clicked on the File Window on the left.
 
The Sync Gradle button is that green-blue circle with a solid circle inside and a green arrow pointing down.  Go Figure!  Its basically a sort of I’ve-made-some-changes-to-the-AndroidManifest-and/or-other-project-wide-parameters-which-require-project-workspace-reindexing (phew) button!
 
The Run button is of course the one used to build and launch the app in the emulator or device.
 
The Green=AOk button tells you all file and project inconsistencies have been resolved and that the project will build and run.  Sometimes you have resolved coding issues and this box is still red.  Just tap on the Sync Gradle button mentioned before and Just Like That, Its Magic…AOk!
 
Great now let’s look at some code…
 
Imports
 
If you expand the imports “+” sign in the MainActivity window’s left edge, you will get a list of what imports a project comes with.  Let’s just make sure they look like this:
 

import android.app.Activity;
import android.os.Bundle;
import com.google.android.glass.app.Card;

The last import as you can see is what allows us to create a Card instance, which is what Glass apps are based on.  This is what you put your info into in a Glass app.

Code

Next, simply add this method inside your public class MainActivity extends Activity statement:

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

Card card = new Card(this);
card.setText(“你 好 吗”);
card.setFootnote(“santiapps.com”);

setContentView(card.toView());
}

Here we are creating a new card, setting its text and footnote properties and setting its view to the Activity’s ContentView or the main view.

Voila!  Connect your Glass to your USB port, make sure to set it to Debug Mode ON and Run the app.  It will build and install on your Glass.

Tweak

Now let’s tweak it.  In your AndroidManifest, declare this intent by making that file look like this:

<?xml version=”1.0″ encoding=”utf-8″?>
<manifest xmlns:android=”http://schemas.android.com/apk/res/android”
package=”com.santiapps.glassapp” >

<application
android:allowBackup=”true”
android:icon=”@drawable/ic_launcher”
android:label=”@string/app_name” >
<activity
android:name=”com.santiapps.glassapp.MainActivity”
android:label=”@string/app_name” >
<intent-filter>
<action android:name=”com.google.android.glass.action.VOICE_TRIGGER” />
</intent-filter>
<meta-data android:name=”com.google.android.glass.VoiceTrigger”
android:resource=”@xml/voice_trigger” />
</activity>
</application>

</manifest>

I’ve pasted my entire file here so as to clear up as much as I can.  Basically you just need to add the intent-filter and its metadata elements.

Let’s make it more interesting!  As with our First Android App tutorial, let’s add some user interaction in Part 2.

First Android App – Part 5

My First Android App

 

Android Studio New Main Layout First Android App Tutorial by Marcio Valenzuela Santiapps.com
Android Studio New Main Layout First Android App Tutorial

Android is based on Java much like iOS is based on ObjectiveC.  If you are coming from an iOS background, itll be a bit jarring at first.  Even though ObjC “comes from” C, C formats are a bit different.  So I thought Id start with that first.

ObjC:

[myObject methodForDoingThis:someParameter];

is a method call which refers to this declared method:

-(void)methodForDoingThis: (id)someParameter{

//1. Take the passed in parameter

//2. Do something to with that parameter value

//3. Call some other method…

//4. Or if this were a method that returned an object instead of void

//4. Return a new object value

}

C:

myObject.methodForDoingThis(someParameter);

is a method call which refers to this declared method:

public methodForDoingThis void (id someParameter) {

//1.-4. Same as above

}

So this might throw you off a bit.  Add to that the fact that their IDE, be it Eclipse or AndroidStudio (or others), or rather their project files are a little less UI-friendly.  What I mean is that where in Xcode you have editable code files in Objective C and either a storyboard or NIBs to deal with UI stuff, in Android IDEs you have an AndroidManifest.xml, various layout.xml files and AndroidActivity.java files as well as a few other ones.

Ok, let’s get started with this Hello World App.

First Android App

First let’s create a New Project in Android Studio:

Android Studio New Project First Android App Tutorial by Marcio Valenzuela Santiapps.com
First Android App Tutorial

Here you can give it an Application Name in natural language, this means with spaces or what not.  This is the human readable name.  Then give it a Module Name which is recommended to not have spaces or strange characters other than alphanumerical characters.  Finally create a package name which is in reverse domain notation (com.something.app).  That something can you be company name or just your name.

In the above example we are actually creating an Android Glass project for Google Glass.  The idea is the same, but for this Android App, select API 15 as all three, Minimum, Target && Compile.  Leave the Theme as is and click on Next.

You will be asked about launch icons and just click Next.

Android Studio New Project Launcher Icons First Android App Tutorial by Marcio Valenzuela Santiapps.com
Android Studio New Project Launcher Icons First Android App Tutorial

You will get another screen about Activities, just leave Blank Activity.

Android Studio New Project Blank Activiy First Android App Tutorial by Marcio Valenzuela Santiapps.com
Android Studio New Project Blank Activiy First Android App Tutorial

Finally you might get this window:

Android Studio New Project Fragment Layout First Android App Tutorial by Marcio Valenzuela Santiapps.com
Android Studio New Project Fragment Layout First Android App Tutorial

Here make sure to make that one change, just to make things easier.  We must also remove the MainActivity.java fragment method later.

Once this is done, remember to go ahead to the MainActivity.java class and open it.  We are going to remove one method in here:

public static class PlaceholderFragment extends Fragment {

public PlaceholderFragment() {

}

@Override

public View onCreateView(LayoutInflater inflater, ViewGroup container,

Bundle savedInstanceState) {

View rootView = inflater.inflate(R.layout.fragment_main, container, false);

return rootView;

}

}

Finally you should see a screen much like this:

Android Studio New Main Layout First Android App Tutorial by Marcio Valenzuela Santiapps.com
Android Studio New Main Layout First Android App Tutorial

Let’s review these 10 top pointers:

1. Your Java Classes or Android Activities

2. Layout files in xml format

3. Value files for storing global settings of sorts

4. AndroidManifest is a sort of Central Registry

5. Top level build.gradle file

6. Low level build.gradle file

7. Tab bar for displayed files

8. Sync Gradle file button

9. Run button

10.Green = A-Ok button!

You can run the app now, it won’t do much, but its an app J.

  1. MainActivity is selected and displayed in the editor window.  This contains your default, boilerplate/template created MainActivity Class required for any app.
  1. The layout folder contains your activity_main.xml file.  If you look inside that file you will see an xml file with some parameters and a TextView element.  You will also see a graphical representation of it off to the far right.
  1. The values folder contains more xml files.  Most importantly, the strings.xml which contains global references to string values.  These are used throughout the app to assign string values where needed.
  1. The AndroidManifest.xml file contains some general settings elements for your app such as the package name, launcher icon, activity tags which contain your declared activities and in this case, if the activity has an intent, which is like an action, it must also be specified here.  In our case we will add a Voice Trigger intent filter if there isn’t one already.
  1. Build.gradle files are not to be messed around with much.  This is a new M.O. used by Android Studio to organize files in a workspace.  You must keep in mind here that there is a Top level and Low level build.gradle file so make sure you know which one you are being told to put things in.
  1. The tabs display whatever files you have double clicked on the File Window on the left.
  1. The Sync Gradle button is that green-blue circle with a solid circle inside and a green arrow pointing down.  Go Figure!  Its basically a sort of I’ve-made-some-changes-to-the-AndroidManifest-and/or-other-project-wide-parameters-which-require-project-workspace-reindexing (phew) button!
  1. The Run button is of course the one used to build and launch the app in the emulator or device.
  1. The Green=AOk button tells you all file and project inconsistencies have been resolved and that the project will build and run.  Sometimes you have resolved coding issues and this box is still red.  Just tap on the Sync Gradle button mentioned before and Just Like That, Its Magic…AOk!

Layout Files

These determine where and how objects will be laid out onscreen.  Check out activity_main.xml and make sure it has this:

<?xml version=”1.0″ encoding=”utf-8″?>

<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=”horizontal” >

</LinearLayout>

This simply tells us that we are using a Linear type layout.  And that the width and height should be stretched out to match whatever the parent is.

Now let’s actually add something onscreen by adding this code at the end of the last line but just before the </LinearLayout> closing tag:

<EditText android:id=”@+id/edit_message”

android:layout_width=”wrap_content”

android:layout_height=”wrap_content”

android:hint=”@string/edit_message” />

Ok, now we added an editable text field with id = edit_message which wraps its content and has a hint of placeholder text = whatever is in the edit_message string.

So what IS in the edit_message string?  Well, open your values folder and double click to open strings.xml.  You will see something like this:

<?xml version=”1.0″ encoding=”utf-8″?>

<resources>

<string name=”app_name”>My First App</string>

<string name=”edit_message”>Enter a message</string>

<string name=”button_send”>Send</string>

<string name=”action_settings”>Settings</string>

<string name=”title_activity_main”>MainActivity</string>

</resources>

As you can see here we have a string value for app_name, edit_message, button_send & action_settings.  You probably don’t have button_send but you will add it soon if you don’t.  This is a globally accessible xml values file.  Whenever some object looks for a string with an identifier, such as @string/xxx, it will fetch whatever is in that strings.xml file at that identifier location.

Nothing Magical here, just an app_name global identifier, a placeholder hint for the editable text field we added, one for a button and another for an action_settings action tool bar button.

So let’s add the button by going back to activity_main.xml and adding this code after the EditText block but again, before the closing </LinearLayout> tag:

<Button

        android:layout_width=”wrap_content”

        android:layout_height=”wrap_content”

        android:text=”@string/button_send” />

This should be pretty easy to understand.  This time we are adding a button with its text property set to the string value for button_send.  If you don’t have that button_send string in your strings.xml then add it in now.

Compile and run and you should see this:

Android Studio New Main Layout First Android App Tutorial by Marcio Valenzuela Santiapps.com
Android Studio New Main Layout First Android App Tutorial

As you can see they are ONLY as large as they have to be.  Both the EditText and the Button objects are only as large as their content is.

The only problem is that the text in the EditText field is, well, editable, thus it could grow longer or shrink.  We must make a small adjustment to account for this by modifying some properties of the EditText object:

<EditText

android:layout_weight=”1″

android:layout_width=”0dp”

… />

This means only add or edit the fields shown here (the rest, …, stays as is).  The layout_weight property states how much space the EditText field should take up relative to those objects around it.  So basically if EditText is 1 and Button is 1, the total is 2, out of which each will take half.

The final layout should look like this:

<?xml version=”1.0″ encoding=”utf-8″?>

<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=”horizontal”>

<EditText android:id=”@+id/edit_message”

android:layout_weight=”1″

android:layout_width=”0dp”

android:layout_height=”wrap_content”

android:hint=”@string/edit_message” />

<Button

android:layout_width=”wrap_content”

android:layout_height=”wrap_content”

android:text=”@string/button_send” />

</LinearLayout>

Build & Run and see your app in all its splendor! Remember to check out Part 2 for a little user interaction before we move onto building Google Glass apps!