Building Apps with Android Studio

jim-wilson-v5

Welcome to this review of the Pluralsight course Android for .NET Developers: 2 Building Apps with Android Studio by Jim Wilson

Jim has over 30 years of software engineering experience, with the past 15 years heavily focused on creating mobile device and location-based solutions. He has co-founded multiple software-related startups and has served in a consulting role at several more.

After nearly a decade as a Microsoft Device Application Development MVP, Jim now focuses on developing Android and iOS device applications. Jim’s passion is mentoring software developers. He blogs at hedgehogjim.wordpress.com.

This is the second course in a series of four courses which Jim has created for Pluralsight. I have also reviewed his first course which is Android For .NET Developers 1: Getting Started. If you are completely new to Android you should begin there. But if you have some previous experience with Android Development you can start here.

Activities and the Options Menu

Course Prerequisites and Expectations

Jim says ideally you’ve watched the previous course, but if you’ve decided to skip that and start here, you need to understand things such as:

  • How ADB server and ADB Daemon work
  • How the ADB command line utility functions work
  • The role of DDMS
  • The role of logcat
  • One of the IDEs – either Eclipse or Android Studio

Jim considers Android Studio to be the future of Android development, which is why this series focuses on this particular IDE.

Android Studio was only at the Early Access Preview v0.1.1 stage when this course was recorded, so some things have changed since then.

Anatomy of An Activity

Jim says the Activity class is probably the most important class to understand. It’s our primary UI class and we can think of it as a form.

It’s usually full screen, but it’s not responsible for every element on the screen.

We see a screenshot with a status bar at the top and a navigation bar at the bottom. The activity is not responsible for either of these things. They belong to the operating system.

An activity has an Action Bar, usually at the top. This has an Action overflow button which is displayed as three vertical dots on the right hand side. This is a menu button.

An activity also has a layout area. What we normally call a Control in .NET is called a View in Android.

Jim gives a quick description of what he’s going to do in the next clip: inflating an XML file and populating a menu.

Walkthrough: Anatomy of An Activity

Jim runs through the steps for starting a new project in Android Studio, and chooses a blank activity.

He says a key difference between Java and C# is in Java all methods are implicitly overridable: unless they are marked as final they can be overridden.

Java has annotations, and these are like attributes in .NET. The image below shows an Override annotation in yellow:

override

This annotation just informs the compiler that the method underneath is overriding something in the base class.

The compiler will then throw an error if it finds the method does not actually override a base class.

Android Studio will automatically put this annotation in for us when we override a method.

Jim explains the basic code this has been auto-generated for us by Android Studio, including the XML.

Using resources simplifies the task of adapting to the differences between the various devices with different screen sizes and languages.

Options Menu

The options menu displays when the user click the Action overflow button in the top right hand corner.

There are two aspects:

  • Describing the menu (creating a definition)
  • Handling the selection when the user chooses their selection

Walkthrough: Options Menu Definition

In this lesson, Jim adds two menu items to main.xml

We see that Android Studio has good auto-completion so we rarely need to type out the full names.

Jim types id and presses enter and we get android:id=""

When referencing a resource from a resource file, we must prefix it with an @ symbol, and we can automatically create a resource by using the + symbol. So we have:

 android:id="@+id/action_other"

At a minimum we need id and title properties for the item element. When the item element is closed, we see that we can select the title and press Alt and Enter to bring up a new menu with the option to extract string resource:

extractresource

This is then added as a resource in strings.xml. Jim talks about the yellow light bulbs that show up. This is the same kind of experience that you get in ReSharper, which makes sense because JetBrains make both IntelliJ and ReSharper.

Walkthrough: Options Menu Handling

Jim writes a new method in MainActivity.java which displays a toast message for the “Other” menu option, and a new method for the exit button:

public void onClickMenuOther(MenuItem item) {
    Toast toast = Toast.makeText(this, "Other Click", Toast.LENGTH_LONG);
    toast.show();
}

public void onClickMenuExit(MenuItem item) {
    finish(); //close the activity
}

Now how do we wire up this code to the buttons? There’s two ways to do it, and Jim shows both of them.

The easiest way is to just add the property android:onClick and specify the name of the method to invoke.

The problem with this is the compiler does nothing to verify that the method exists. Jim makes a typo and we see the runtime error “Unfortunately, InteractiveUI has stopped.”

Jim also says this technique creates difficulties with Fragments, which is a UI concept explained in a future course in the series, and also in Gill Cleeren’s Building your First Xamarin.Android app course. OnClick does not work with Fragments.

So Jim shows us the preferred technique. He overrides onOptionsItemSelected. This function is called whenever the user selects a menu item.

@Override
public boolean onOptionsItemSelected(MenuItem item) {
boolean handled = false;
   int id = item.getItemId();
switch (id) {

        case: R.id.action_other:
handled = true;

               break;
        case: R.id.action_exit:
handled = true;

               break;
        default:
handled = super.onOptionsItemSelected(item);

       }
   return handled;
}

Continue to Part Two – Adding Activities

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s