Update 11/10/14: We have updated this tutorial for the most recent version of Android Studio – check out the updated version here.
“But when does the Android version come out?”
–The first blog comment after any iOS app gets announced.
Clearly there’s a demand for Android app development, and it’s turning the platform with the lovable green mascot into more and more of a strong first choice rather than just a secondary option to iOS.
With over one billion devices activated, Android is an exciting space to make apps to help you communicate, organize, educate, entertain or anything else you’re passionate about.
If that’s not enough, here are a few more reasons to learn Android:
- You’ll be plugged into the open source platform with (at the time of press) the largest market share of smart devices worldwide.
- Android’s policies on device provisioning and app submission are more open than Apple’s, meaning that once you complete your first app—as you’ll do in this tutorial—you and your friends can enjoy it on your devices right away!
- If you have experience developing for iOS, you can become well-versed in the ways that the two platforms coincide and differ (we’ll discuss a few in this tutorial) and what you like about each. Then you’ll have more tools at your disposal for your next mobile project.
- It’s not just the iPhone anymore. There are so many smartphones, tablets, glasses, and watches out there, coming from so many manufacturers, and they’re all trying to jump into the game. You don’t have to be any sort of market analyst to know that there are a few important platforms and Android is one of them.
So if you’ve been intent on, thinking about, or simply playing with the idea of learning Android… Make Your First Android App is the tutorial series for you!
There aren’t any prerequisites to start. You’ll learn how to set up all the tools you need to become an Android developer-in-training. Then you’ll put together your own full-fledged Android app from scratch! This app will, when completed, help you get details about a book you’re interested in using online search sources.
By the end of Part Three of this series, your app will include useful features like:
- Saving app data locally on the phone.
- Loading and interacting with a dynamic list of data.
- Accessing an online data source.
- Sharing links through social networks.
Android, and several popular frameworks for it, make all of these features really simple to implement. Why not start learning this easy and powerful platform for yourself, today?
How does Android development go down? First, the zoomed-out basics:
- You’ll write your programming—what you want your app to do—in Java files and design your layouts—how you want your app to look—in XML files.
- Once your app is ready, you’ll use a build tool to compile all the project files and package them together into a
.apkfile that you can run on Android devices and/or submit to Google Play.
- All of the files you used to put your app together are managed by an Integrated Development Environment (IDE). The IDE is the program you will open to edit your code files and to manage your projects.
- The standard IDE for Android used to be Eclipse, but this is now being replaced by Google’s own Android Studio.
If you zoom in (metaphorically), you’ll find more in-depth processes going on behind the scenes during all of the above steps. For example, advanced users will want to investigate the role of the Dalvik Virtual Machine and it’s new replacement, ART.
But for now, let’s move on. Here’s what you’re going to accomplish in this part of the tutorial:
- Download and install Android Studio.
- Set up testing on devices and emulators.
- Create a simple “Hello World!” Android app that prints to your device’s screen.
- Make small edits to the app so it can congratulate you by name.
In the second part of this tutorial, you’ll make an app that records a message you type, adds it to a list, saves it in memory and shares it with friends. Along the way, you’ll learn how to add and configure various UI elements such as
Finally, the third part will cover how to make an app to search an online database of books, display and share the cover images of the search results, and navigate between the different screens of your new app!
Installing Android Studio
It’s really tempting to jump in, start writing code right away and figure out the various Android features as quickly as possible. And you will get there soon! But one of the most important parts of getting started with a new platform is setting up your environment.
Especially for beginners, it’s important to take your time here and follow each step methodically. Even if you follow the steps perfectly, you may have to troubleshoot some small issue, depending on your system configuration or product versions.
It’s important to not let any roadblocks prevent you from your ultimate goal of learning Android. Even seasoned professionals have been known to have some trouble with the new Android Studio.
file X not foundas a final answer. 90% of this mindset is simply training yourself to keep trying till you find a solution.
With all of this in mind, let’s quickly check that you have the Java Development Kit (JDK) installed. You might already, even if you don’t know for sure.
To check, you’ll use the Terminal. If you’re unfamiliar with the Terminal, you may want to read a good introductory tutorial about working with the Terminal.
In a nutshell, your Terminal is like looking under your car’s hood; it’s how you really get to know the machine face-to-face, without any complex graphical interface in the middle. You can find the Terminal app quite easily on a Mac using Spotlight. Search for terminal and click the top hit to run Terminal.
Once you have the Terminal open, type in
java -version. You should see some output that mentions a version number, like below.
If you don’t have the JDK installed, your Terminal will tell you
command not found. If that’s the case, you should download the JDK from Oracle.
When ready, head over to the Android Studio page and find the button to download the correct version for your platform.
Google is constantly updating this page, so the version you see may be newer than the screenshot above. Click the Download button and you’ll be asked to agree to the Terms and Conditions:
After reading these carefully (as you always do) and accepting, your download begins. It will likely take a few minutes. Once done, you can install Android Studio similar to any other program. The original download page contains the familiar Mac and Windows installation instructions in case you need a refresher.
Now that you’ve installed Studio, let’s fire it up! Launch Android Studio. It may ask you if you want to import any settings:
Since you’re starting from scratch, if given the choice just select I do not have a previous version… and move on. You’re presented with the nice loading screen, complete with all the various phone and tablet screen sizes in the background. More on screen size diversity later. ;]
Once that loads, you are greeted with a Welcome screen.
Even though you just downloaded Studio, you might not actually have the latest version. Right away, you need to click check for updates and if necessary, follow any instructions to get the latest version.
Sometimes, Studio automatically lets you know that there’s an update via a screen similar to this:
In that case, always choose Update and Restart.
Great! As all programmers in the movies say when they are successfully greeted with a new interface: We’re in!
Optional/Advanced: Moving from Eclipse
You can skip to the next section if you are new to Android development.
Android Studio is a new IDE that Google is constantly updating. If you are already involved in Android development, you are probably using Eclipse. For the transition, the Android team has put together a helpful guide on how to move from Eclipse to Studio.
The process can still be a little bumpy, even for advanced users, so the best advice I can offer on that front is to make sure that your version of Gradle is up-to-date, as well as your versions of the Android Platform Tools and Studio itself.
It’s also important to check the
PATH of your Android software development kit (SDK) when switching to Studio. Otherwise, Studio may not be able to find the right Android SDK or Build Tools!
Creating OMG Android
It’s time. Let’s make your first project.
You’ll start simple. Many first programs are called “Hello World.” Let’s follow that tradition and then make a few small edits so that the app uses your name to greet you. By the end, you’ll be able to load your new app onto a device and show it to your friends!
Android Studio has a nice little step-by-step tool to help you make your project. Click New Project from the Welcome to Android Studio screen:
Studio will present you with a screen like the following:
Fill it out according to the screenshot above. Feel free to replace “example” in your Package name with something else to remove the warning at the bottom of the screen.
You can set the Project location to any location on your hard drive – you do not need to follow the screenshot for that one :]
For the SDK drop downs, pay attention to the Description section at the bottom of the dialog. It explains what each setting is for.
Set the Minimum required SDK as shown in the screenshot. This sets the minimum version of Android needed to run your app. Selecting this value for your own projects is a matter of balancing the SDK capabilities you want and the devices you want to support.
For your first app, I recommend using API version 14, which is Android 4.0 Ice Cream Sandwich. Every app will have different requirements and you may want to choose something else, depending on the situation.
For more information on API versions and their use, check the Android Dashboards, which are constantly updated.
You’ll note that a fair number of devices are still on API level 10, Gingerbread. In case you’re considering supporting Gingerbread in your project, do note that that number is shrinking and would require you to make some accommodations in your app design that often aren’t worth it.
Getting back to the dialog, Target SDK and Compile With aren’t crucial here. They should each just be the highest value possible at the time. As new SDK versions come out, this number will rise.
Click Next and you’ll see more options.
This screen creates the default app icon for you. The default values are fine for now — you’ll change it later. So click Next.
Another step in the creation process appears:
This screen offers a few different options that define how your app UI is set up. You can simply accept the default, which creates an (almost) blank screen with an action bar. The action bar is one of the reasons why you set the minimum SDK to Ice Cream Sandwich: it’s the way that modern Android apps help users easily navigate and select app options and it’s only available from API level 14 onwards. Click Next.
You are now presented with the final step in the process:
Before you click Finish, take a moment here, because you’re starting to get hints of what your program is going to look like and of terms you should know going forward.
Activity Name. This is the first mention you’ve seen of the word
Activity, but it certainly won’t be the last. In Android, an
Activityis usually thought of as a “screen” in your app. It could be fullscreen or it could be a partial-screen pop-up. It is very flexible. If you want your user interacting with something in your app, that’s at some level happening via an
Activity.Note: When Android Studio creates your
MainActivity, what it’s doing is making a subclass of
Activity. Those familiar with object-oriented programming will know what this is, but for newcomers, this basically means that your
MainActivityis going to be a customized version of
Activitythat acts just like the default one, handling things like its lifecycle and the user interface display.
Layout Name. You’re going to define your
MainActivityin Java, but the layout of everything it will show to the user is defined in a special sort of Android XML. You will learn how to read and edit those files soon.
- Fragment Layout Name. A fragment is a little like a sub-activity, and can be a useful part of your app design. I won’t go into fragments much in this tutorial—in fact, one of the things you’ll do is remove the one that the New Project wizard makes for you—but I encourage you to further investigate how to effectively use fragments after you finish this tutorial.
- Navigation Type. For now, just choose None. You won’t need any navigation for a little while, and when you do, it’ll be smarter to build it yourself, from scratch.
Click Finish. Android Studio takes this as its cue to go do a bunch of behind-the-scenes operations and create your project. As it shoots out some descriptions of what it’s doing from time to time, you may notice it say something like the following:
You see your project name, which is familiar. But then there is this Gradle word, and then a mention of Maven in the URL. The benefit of having a modern IDE like Android Studio is that it handles a lot for you. But as you’re just beginning to learn Studio, it’s good to know, in general, what it’s doing for you.
- Gradle is a new build tool that is easy to use, but it also contains a lot of advanced options if you investigate it further. Basically, it takes your Java code and XML layouts, and uses the latest Android build tools to create the app package file, known as an APK file. You can customize your configurations to have development or production versions of the app that behave differently, or add dependencies for third-party libraries.
- Maven is another project build tool, and it can also refer to the Maven Central repository of java libraries. It is absurdly easy to use Gradle and Maven Central in concert with Android Studio to incorporate all sorts of functionality from the Android development community. Those with an iOS background will know how cool this sort of thing can be from using the CocoaPods tool. You’ll learn more about Maven in Part Three of the tutorial.
After a while, Android Studio will finish building your project. The project is pretty empty, of course, but it still has everything it needs already set up so that it can be launched on an Android device or emulator. You will be dropped off in this spot:
You’re looking at a layout file in XML format. Before you get into any programming, let’s talk about how you’re going to get this app running. It’s time to say “Hello world!”
Running on an Emulator or Device
All right: You’ve got Android Studio and you’ve created an app. Let’s see this app live on an emulator or device!
Press Edit Configurations. You’ll find it marked in the image below. Note that some windows have it moved to the right a little and the option may take a minute to appear as Studio finishes all processes.
A new dialog will appear showing you some options for Run/Debug Configurations.
Well, you want to run your app, so you must be in the right place. Look where it says Target Device. Here’s where you tell Studio where you want to run the app.
At this point, it would be best if you have an Android device handy — more about readying your device in just a bit. But for those without one, you also have the choice to run on an emulator.
Now, let me say a word about the emulator. Android Studio comes free with the ability to set up a software-based Android device on your computer and run apps on it, browse websites, debug and everything. This capability is known as the Android emulator.
You can set up multiple emulators if you want and you can set the screen size and platform version for each new emulator to whatever you like. This is great, as you can avoid having to buy multiple devices for testing.
The downside to the emulator is a big one. Unfortunately, the emulator is painfully slow. For those coming from iOS development, you will really start to appreciate the iOS Simulator after using the Android emulator.
All of that being said… let’s set up an emulator anyway.
In Target Device, click the Emulator option, followed by the button to the right of the drop-down.
Note: You need to select the button to the right only if you want to create a new emulator. If your Android development set up already contains some emulators you’ve created, then you can simply select one of those from the drop down instead.
This opens the Android Virtual Device (AVD) Manager. This is the place where you can create, edit, and delete the various emulators you might want to work with. Since your list is probably empty (if you are just starting Android development), you need to create your first one. Click New.
Now you see a bunch of options to create your emulator:
There are options to base your virtual device off a real one (I chose Nexus 4), set its Target API Level (I chose 4.2.2 Jelly Bean) and so on. You have some flexibility here, but if you’re not sure what to choose, you can copy my settings from the image above. Once you’re ready, click OK.
You’ll see a brief summary pop-up of the options you just chose. Dismiss that, and then your newly-created emulator is on the list and ready to run your app!
Now, close the AVD Manager to go back to the Run/Debug Configurations dialog. You now have the option to choose your new AVD as the default configuration:
Set the new AVD as the default configuration, click Apply and then OK. You’re back at Android Studio’s main view. Now that you’ve configure everything, there’s but one step left…
Click the Run button. It looks like a “play” icon.
You may have to wait a while as the emulator loads, and you may even need to try it more than once for the emulator to get it right, but once it’s ready, you should see something like this:
Congratulations! You made your first Android app!
Android apps used to be something you only interacted with on the consumer side, but now… you’re a creator. There’s power and possibility in that. It’s worth taking a moment to think about where you might want to go with this newfound ability.
When you’re ready, you need to revisit something I quickly glossed over earlier — testing on devices.
Navigating the Android Candy Store
One of Android’s advantages, the diversity of devices that can run the platform, is also a major complication for developers. You have to consider just about everything, such as the items on the following non-exhaustive list, to be variable:
- Screen sizes, both physically and in terms of pixels.
- Screen density, or the number of pixels per mm.
- The ratio of the screen width to the screen height.
- The number of simultaneous touches the touchscreen can register.
- The quantity and positioning (front vs. back) of cameras.
- Bluetooth capabilities.
- Platform and software versions.
If you want to make an app that will run on a hundred different devices, how can you tell if it’s going to work? Well, there are six main strategies I can recommend:
- Nail down your target.
- Filter your manifest.
- Check Android’s best practices.
- Pick at least one representative device.
- Fake other screens on your device.
Let’s go through them one by one.
1. Nail down your target.
Does your app have some specific, indispensable hardware or software requirement? What about nice-to-haves? Take stock of device needs early, so that if your app is camera-based, or communicates with a specific product through Bluetooth, you’re ready to target your app to devices with those capabilities.
Check the often-updated Android Dashboards to see the prevalence of platform versions as well as screen sizes and densities. You can see, for example, that your choice in this tutorial to only support Ice Cream Sandwich and higher will (at press time) lose you about a quarter of Android users. It’s a reasonable sacrifice in your case.
2. Filter your manifest.
Once you’ve determined you app’s requirements, you need to let Android know about them. That way, the Google Play Store can keep non-compatible devices from purchasing your app.
This will save you a lot of frustration and poor reviews from users who might have downloaded your app, only to find a strange error awaiting them. It won’t, however, absolve you from communicating in any marketing materials the list of devices your users should expect to be able to run your app on.
The device features you need should be listed in a file. This file, available in every project, is called the Android manifest. This is an XML file that contains all the high-level info about your app. Remember when you chose API 14: Ice Cream Sandwich as the minimum SDK when you created the OMG Android project? That choice is now reflected in your Android manifest.
AndroidManifest.xml on the left side of the Android Studio window and double-click it to take a look. Can you spot where the code sets the SDK version?
The code sets the minimum and target SDKs via the
uses-sdk tag. The target SDK value just lets Android know that you’ve tested your app up to that version and everything’s shiny, not to fret. The target SDK should always be the newest possible SDK.
Just as the
uses-sdk tag tells Android to filter out devices that don’t meet the minimum SDK, you can use the
uses-feature tag to allow only qualified devices. For a different app — not for this demo — one would add this line to filter out devices without a camera:
<uses-feature android:name="android.hardware.camera" />
3. Check Android’s best practices.
There’s a great list of best practices to address compatibility issues that’s available on the Android site. Some of the most useful tips include:
- Use density-independent measurements in your layouts, so your specifications will scale along with the screen.
- When putting together layouts, use relative widths and heights instead of absolute values.
- Putting in several versions of the same image file, tailored to different screen densities, can also be a good idea.
The tutorial will discuss these in more detail in Part Two, when you lay out views in XML.
When you set up your emulator earlier, you saw how many options you have to work with different screen sizes. You also saw how slow the emulator can be. It is a flawed tool, but it can be useful when you have an issue under a specific hardware configuration but do not have the necessary hardware to replicate the issue.
5. Pick at least one representative device.
The most expensive option, unless you already own an Android device, is to go out and buy one. I recommend trying to find a used, unlocked device from the Google Nexus series. If your budget is tight, I’d go two generations back, which at the moment would mean a Galaxy Nexus. Of course, if you’ve got the money, you can go for the new snazzy Nexus 5!
With all the time you will save avoiding the emulator, if you continue on with Android (which I hope you will!), it will definitely be a worthwhile investment.
If you have a device, you don’t need any silly provisioning profiles. You just need to turn on USB debugging for your device. Sometimes the checkbox option is available just by going to Settings > Developer Options on your device. Check these instructions for more details.
Other times, you have to do some weird shenanigans. I can’t make this up! A direct quote from Android: “On Android 4.2 and newer, Developer options is hidden by default. To make it available, go to Settings > About phone and tap Build number seven times. Return to the previous screen to find Developer options.”
No matter if you had to do a little dance or whistle a perfect C to reveal the option, once USB debugging is on, you can go back to the Run/Debug Configurations screen and set your Target Device to USB Device. This will serve you well for the rest of the tutorial.
6. Fake other screens on your device.
If you are lucky enough to have a device and have enabled USB Debugging on it, then you have another testing advantage: you can use it to “fake” other screen sizes. Here’s how.
Open your Terminal as you did earlier, when you checked for the JDK. Then locate a tool called the Android Debug Bridge (adb). With early versions of Android Studio, it will be in the /Applications/Android\ Studio.app/sdk/platform-tools directory, or wherever you installed your copy of Android Studio, if elsewhere. Later versions of Android Studio do not come with the Android SDK bundled in and so, you might have your Android SDK installed in a totally different location.
You can use the following command at the Terminal prompt to see if adb is available in your system PATH:
The result should be something like this:
Note: If the
adb version command results in an error message, your Android SDK folder is probably not in your PATH. Then you’d need to find the exact location of the Android SDK and change to the correct folder (as in the first steps in the screenshot above) before you can use any adb commands.
adb version command is just an example to show you how to call adb from the command line. It can run all sorts of commands and you can get a list of available commands by typing
If not already set up, you can optionally add the Android SDK platform tools folder to your $PATH so that you can run adb from anywhere on your system. I’d suggest that you do this only if you are familiar with UNIX and feel comfortable doing this.
With your device plugged in and with its screen turned off, type the following:
adb shell wm size 640x480
And then type:
adb shell wm density 160
This represents a screen size of 640×480 pixels with 160 pixels per inch pixel density.
When you turn the device screen back on, you should see that the resolution has changed to match the new values you entered!
Feel free to turn the screen off, try another set of dimensions and turn it back on again.
To return your device to its normal settings, type:
adb shell wm size reset
And then type:
adb shell wm density reset
So you could get a full-size tablet (like a Nexus 10, perhaps) and then easily simulate all sorts of smaller devices, without having to use the emulator!
I hope that information helps you navigate the Gingerbreads, KitKats, Jelly Beans, and all the other varieties of Android candy. Now, back to the app at hand…
So… WHY did that work?
You’ve got your first app behind you. To start making changes and adding cool features, it’s necessary to get a working knowledge of what’s going on behind the scenes.
Take a look at the Project section of Android Studio, with the files and folders on the left side of the screen. You may need to press the little tab on the edge (see below) if the project explorer isn’t visible at the moment.
Browse around for a few minutes without any explicit instructions from me, expanding and collapsing folders and double-clicking on files to see their contents in the main window. If you notice any trends, great. If it all still looks cryptic, not to worry!
Android Project Structure: The Team
Every great team is composed of people who play different roles. Do you want to do the job right? You need the right team. Android Projects have a few key elements and each has a role to play:
- Java: The Professional
- Resources: The Artist
- AndroidManifest.xml: The Boss
- Intent: The Job itself
Java: The Professional
It’s the job of your Java code to get things done. Your code is all going to be in the src\main\java directory under your main project folder. I’ll give you all the code you need to complete this tutorial, but if you want to learn or refresh your Java knowledge, here is a nice online interactive tutorial.
It will be worth your time to learn more and more Java as you explore Android development. Your team is relying on the professional.
Resources: The Artist
It’s not enough to just get the job done. It needs to be done in style. Your app is never going to stand out unless it has great icons and images, well-designed layouts, engaging copy text, and maybe even some smooth animations.
Initially, the src\main\res (Resources) folder contains:
- Drawable folders that hold images — just the default launch icon for now.
- The layout folder with XML that represents the screen designs.
- The menu folder with XML of the items that will appear on the Action Bar. More on that later.
- The values folder with XML containing dimensions, strings, and styles.
AndroidManifest.xml: The Boss
Someone’s got to call the shots. That “someone” would be the Android manifest. This XML file informs your system of the app’s hardware and software requirements and contains your app’s name, icon, and version.
The manifest also filters the
Intents coming in. You need a job done by your app? Talk to the boss first. Now, more about the jobs themselves…
Intent: The Job itself
Want to show the user a screen? Want to navigate to a website? Whatever the job is, in Android it is going to take the form of an
Intent. If you come from an iOS background, pay close attention because this is a very “Android” concept.
The Android system knows that you will potentially have a lot of apps on your device, and wants to make it easy for them to talk to each other. So, it allows you to send and receive what are essentially requests for jobs to be done.
A job could get picked up by your app’s own boss (the manifest) or another app. When creating an
Intent, it’s up to you to either write it very generally to have the option of picking from several apps to perform the job (implicit), or very specifically to follow a certain path (explicit). You’ll see an example of each type if
Intent later in this tutorial.
For an immediate example, your app already has an
MainActivity. Your manifest has it labeled with an intent filter that causes the
MainActivity to launch when the user selects the app icon from their home screen. You could potentially move that filter to another
Activity and then that activity would launch instead of
MainActivity. Basically, the app does whatever the boss says.
If you don’t fully grasp everything about
Intents right away, don’t worry. Just keep the concept in mind as you see
Intents throughout the code, and eventually you will start to get an idea of their potential.
Putting a Personal Stamp on Your App
You’ve made your first app, but what’s the first thing you always want to put on any of your work? No, not a bird… your name!
res/values/strings.xml and double-click the file. When you open the file, you’ll see three string resources in XML.
These resources are accessed in different places, but it is very convenient to have all of the text used in your app in one file. If you need to translate it, or if your marketing coworker tells you to remove all the nerdy references from your app, it will be easy to make all the changes here.
Change the hello_world string. That string is the one that the app displays on the screen. So, change it to something more personal that incorporates your own name – something like:
<string name="hello_world">Matt is learning Android!</string>
So remember: When you launch the app, you’re essentially doing the same thing as sending a launch
Intent to the manifest. As the boss, the manifest takes a look at the
Intent and decides it has the perfect fit for the job:
MainActivity. The Java does the heavy lifting of opening the screen, but for what to display it goes and asks the artist, eventually leading to strings.xml.
Click Run. When the app launches again, you’ll see your personalized message!
Congratulations! If you have a device, you can go around showing off your new app to your friends or take a screenshot from the emulator and send it to them.
You’ve entered the world of Android. You’ve set up your development environment (no easy task!), created your first app, run it on an emulator or device, and changed the app so that it specifically addresses you. Great job!
Updating With the SDK Manager
This tutorial will work with whatever SDK version you downloaded with Android Studio, but it is also a good idea to always keep your SDK versions and Android Platform Tools up-to-date.
Click the button in Studio to access the SDK Manager, which allows you to easily update your Android SDK components.
You can also download older SDKs and other tools, if you need them for future projects, via the SDK Manager.
Where to Go From Here?
There’s a lot still to come, but here are some extra tips to think about before jumping into the next part of the tutorial:
- Follow Android. The development community around any language or framework can be its strongest asset. It’s never too soon or too late to start checking out Google I/O videos, following the Android Developers blog or watching DevBytes videos.
- Get the full source for this section of the tutorial on GitHub or as a .zip.
- Post any comments/suggestions/questions below.
- Stick around for the second and third sections of the tutorial! Thank you!
The Android robot is reproduced or modified from work created and shared by Google and used according to terms described in the Creative Commons 3.0 Attribution License.