Learning Java by Building Android Games
上QQ阅读APP看书,第一时间看更新

Our first game project

Now we will get straight down to actually doing something with Android Studio. Run Android Studio by double-clicking on the Android Studio icon either on your desktop's start menu or in the folder where you installed it.

Note

If you get any errors in a dialog box mentioning Privilege elevation, then try running Android Studio with administrator privileges. To do this, find the Android Studio icon by clicking on the Windows Start button and searching for Android Studio. Now right-click on the icon and click on Run as administrator. Do this every time you run Android Studio.

Preparing Android Studio

So with Android Studio and Java installed, we just need to add the latest versions of the Android API that we will use to make our first game. Here is what to do to install the API:

  1. From the menu bar at the top of the Android Studio UI, navigate to Tools | Android | SDK Manager. Scroll down in the Android SDK Manager window and select the checkbox for Android 4.4.2 (API 19).

    Note

    Note that because Android is evolving so quickly, by the time you read this chapter, there might be APIs newer than 19—20, 21, and so on. If this is the case for you, select the newer (higher numbered) API instead.

    Preparing Android Studio
  2. Click on Install Packages.
  3. On the next screen, click on the Accept license checkbox and then click on the Install button. Android Studio will download and install the appropriate packages.

What we just did was setting up Android Studio to make available the latest, prewritten code called an API that we will interact with throughout the book.

Building the project

  1. Click on New Project... as indicated in the following screenshot:
    Building the project
  2. The Create New Project configuration window will appear. Fill in the Application name field with Math Game Chapter 2 and Company Domain with packtpub.com (or you could use your own company website name here), as shown in the following screenshot:
    Building the project
  3. Now click on the Next button. On the next screen, check that the Phone and Tablet checkbox has a tick in it. Now we have to choose the earliest version of Android we want to build our app for. Go ahead and play with a few options in the drop-down selector. You will see that the earlier the version we select, the greater is the percentage of devices our app can support. However, the trade-off here is that the earlier the version we select, the fewer are the cutting-edge Android features available in our apps. A good balance is to select API 8: Android 2.2 (Froyo). Go ahead and do that now as shown in the next screenshot:
    Building the project
  4. Click on Next. Now select Blank Activity as shown in the next screenshot and click on Next again:
    Building the project
  5. On the next screen, simply change Activity Name to MainActivity and click on Finish.

    Tip

    By default, Android Studio shows a Tip of the day dialog every time it starts. Some of the tips might not make sense while you are still learning Java but many of them are really useful and reveal great shortcuts and other time savers. It is well worth taking a few seconds to read them when they show up. As already discussed, Android Studio is built from IntelliJ IDEA, and you can find a complete list of keyboard shortcuts at http://www.jetbrains.com/idea/webhelp/keyboard-shortcuts-you-cannot-miss.html.

  6. Clear Tip of the day by clicking on Close.

If you are completely new to programming, then the code, options, and files might seem a bit daunting. Don't worry; stick to them as we don't need to concern ourselves with most of them in order to learn Java. When the time does come to interact with the finer details, we will do things one step at a time.

It might be hard to believe that at this stage, but we have just created our first working app. We could build and run it on an Android device, and soon we will.

Let's take a deeper look at Android Studio before we progress with our game.