Saturday, January 17, 2015

Android Development - Hello World Application

If you know Java, you can learn Android development. In this blog post, we'll create a very simple Android application in Android Studio and run it with an Android emulator. To get started, download the Android Studio installer here:

Take note of the system requirements, most importantly being the Java Development Kit (JDK).

Now that you've installed Android Studio, let's create a simple application for Android. Start up Android Studio and click the option for "Start a new Android Studio project".

Welcome to Android Studio Dialog

The next dialog prompts you to name your application and provide the Company Domain. For all intents and purposes, this is simply the package name for your Java classes. Choose a name for your application and package directory and click the "Next" button.

Android Studio New Project Dialog

The next dialog prompts you for the platform where you want to develop and deploy your application. Android is a very cool platform, in that you can develop apps for TV and wearables (e.g. watches), but for this example, I'm going to select "Phone and Tablet" and choose "API 8: Android 2.2 (Froyo)" for my minimum SDK version. When you develop applications for Android, you specify the minimum and maximum SDK version. For this application, API 8 Froyo will be the minimum SDK version needed to run the application. After you select the platform, click "Next".

Android Studio Select API Version Dialog

Next, you select an activity for you application and click "Next". I'm going to choose a Blank Activity. As you get more experienced with Android development, you'll become familiar with activities and the Activity class. For now, you can think of an activity as a screen or view in your application. By choosing Blank Activity, the application will have a blank screen to start building an application.

Android Studio Activity Dialog

The next dialog prompts you for a name for the activity. I'm going to leave it as MainActivity and click "Finish".

Android Studio - Name Activity Dialog

After clicking the "Finish" button, you'll see a progress dialog, as your application is being created.

Android Studio - Create Project Progress Dialog

When Android Studio is finished building your project, you'll see your new Android application open in the IDE.

Android Studio - IDE Screenshot

Next, let's run the application in an Android emulator. You can start up an emulator from the menu option under Tools-> Android -> AVD Manager. Note that AVD Manager is short for Android Virtual Device Manager.

Android Studio - AVD Manager Menu Option

The AVD Manager dialog prompts you to start a virtual device / emulator. If you don't have a virtual device created, you need to click the button for "Create Virtual Device...". This will allow you to device configuration and Android API version that you want to emulate. For this example, I'm going to use the virtual device that emulates a Nexus 5 using API 21. Because I already have it created, I'll click the green start button on the right side of the device listing, to start the device emulator.

Android Studio - Choose Virtual Device

Next, you'll see a progress dialog, as your device emulator is starting.

Android Studio - Starting Android Virtual Device Progress Dialog

Note that it can take a few minutes to start the emulator, so be patient. After your emulator has started, you'll see the Android welcome screen (just as you'd see if you were powering on an actual Android device). After powering on, you'll see the Android home screen.

Android Studio - Galaxy Nexus Startup Screen

To unlock the welcome screen, just click on the unlock icon and swipe upwards, as you would on an actual Nexus device.

Android Studio - Galaxy Nexus Home Screen

Now that the emulator is running, go back to your Android Studio IDE to run your application on the emulator. From the Android Studio menu, select Run -> Run...

Android Studio - Run Application Menu

You'll be prompted for the device where you want to run the application. Choose the emulator that's already running and click "OK".

Android Studio - Choose Running Device Dialog

After a few seconds, you'll see the application running on the device emulator.

Android Studio - Hello World Application

Congratulations, you've just created your first Android application! In the next Android blog post, I'll talk about the project structure and the files in the project, as well as provide a more involved application example.

No comments:

Post a Comment