Welcome to the Android Studio Tutorial. This is the first article in the android tutorial series and today I’ll show you how to setup your android development environment and create a Android Studio Hello World app.
Table of Contents
Android Studio Tutorial
Since Google has dumped Eclipse and further development will be done in it’s own IDE, we will be using Android Studio as our development environment IDE. We will learn how to install android studio IDE and create a simple hello world application step by step from scratch.
Android Studio Setup
Before you get your hands on Android Studio, download and install it from the link. Note that you need to have JDK 6 or higher installed. If you’re on Windows, launch the .exe file and follow the steps of the setup wizard. If you’re running Mac OS X, mount the disk image by double-clicking it and drag Android Studio to your Applications folder.
Android Studio Hello World
You’re now ready to create your first Android application using Android Studio. When you launch Android Studio for the first time, you should be presented with a welcome screen, offering you a number of choices to get you started.
We’ll go with the New Project option here. However, you can choose Import Project if you’d like to import a project from, for example, Eclipse, into Android Studio. Android Studio will convert the Eclipse project to an Android Studio project, adding the necessary configuration files for you.
If you select Open Project from the list of options, you can open projects created either from the list of recent projects or by browsing the filesystem.
Android Studio Tutorial – New Project
Let’s create a new project using the wizard shown in the Quick Start section of the welcome screen. Enter some name for the application, let’s say “Hello World” and make sure your package name is unique.
If you own some domain say yourdomain.com, then your package could be
com.yourdomain.helloworld. While uniqueness isn’t of concern here, it will be important for an application that needs to be published to play store. Google Play Store distinguishes apps by the package name only.
Click “Next” button again and you’ll be presented with a list of platforms where you want your application to run along with separate SDKs list since different platforms require different SDKs list.
On the next screen you’ll be presented with a list of activity types. The default Blank Activity is good enough for this project.
Click Next. This screen is an Activity configuration screen. Since you asked ADT to create an activity for you, this screen helps you in configuring relevant options.
- Activity Name: Let’s stay with the default name MainActivity.
- Layout Name: ADT that creates activity for you, will also create a layout for you to bind it with the activity.
- Title: It specifies the title of the activity.
- Menu name: This is used to provide menu options. Keep default name only.
Click finish and wait for a few seconds for Gradle to do its magic.
Gradle is an advanced build toolkit that manages dependencies and allows you to define custom build logic. Android Studio uses a Gradle wrapper to fully integrate the Android plugin for Gradle.
The Android plugin for Gradle also runs independent of Android Studio. This means that you can build your Android apps from within Android Studio or from the command line on your machine or on machines where Android Studio is not installed.
Once you click Finish ADT will create your new project and will open Java code and Layout code for you.
By now you might be wondering how do you change your default white theme to the dark theme shown in the snippets. Go to menu Android Studio > Preferences, choose IDE Settings > Appearance. Change the ‘Theme’ from ‘Default’ to ‘Dracula’.
Android SDK Manager
Since this is the first time this IDE is being used, we need to download few more essential pieces using Android SDK Manager. There is a green Android icon in the toolbar on top. Click on it to open the SDK Manager. Choose the components of the latest API that aren’t yet installed. Similarly scroll down and choose all the “Extra” components that aren’t yet installed and then click the “Install … packages”.
This will throw a dialog with license information. Accept using “Accept license” radio button on bottom left. And then choose to Install.
Download may take a long time to finish. Don’t close that window. But return to the IDE.
Now before we jump into any programming, let’s talk about how to get this app running. It’s time to say “Hello world!”
Android Studio Tutorial – Running app on Emulator or Device
Create a new AVD instance using the AVD manager and provide the details asked.
Android Hello World App
Once finished with the creation of virtual device, run the app on the emulator. Congratulations on your first android hello world app. But have you noticed how slow the default emulator is?
Hence it is recommended to use Genymotion plugin or debugging through phone for testing purposes. You can find the steps to integrate Genymotion with Android Studio here. Sign up and create your own virtual devices for a speedy development.
You should keep your phone connected to your PC through USB. While deploying the app, your phone would be shown as an option to which you can deploy. To do this change Edit Configuration > Target Devices >Show Chooser Dialog.
If you are planning to deploy the Android app to your phone, it is essential to turn on USB Debugging in your Android phone.
- On most devices running Android, you can find the option under Settings > Applications > Development.
- On Android 4.0 and newer, it’s in Settings > Developer options.
Click run. The following window should pop up to choose the device.
Android Hello World App Project Structure
It seem almost unbelievable that the simplest Android app you can create involves so many files. Most of the files that have been created are auto-generated and most of the time you don’t need to know anything about them let alone open or edit them.
So let’s focus on the files that matter to us.
For our simple program there are only two important files one that determines the Activity’s behaviour
MainActivity.java and one that determines most of the visual appearance
You can set which activity is the one that the system starts but by default it is the single activity that you created and named when you set up the project.
In this case we named the activity MainActivity and its layout file activity_main – but you could change these defaults.You can see the location of these two important files in the Project window:
MainActivity class in MainActivity.java file extends Activity. It overrides
onCreate() method with a custom call. The custom call starts with a call to the parent class’ (Activity’s) onCreate(). Then it sets the layout to show on the screen using
This line is the most important of all and really the only one that actually does anything. It gets the resource object that represents the layout as defined by the XML file and makes it the current ContentView i.e. it is what’s displayed on the screen.
The layout is referenced using ‘R’, a resources class, that is generated by the build system. R has a reference field for every Android resource in use from this project’s resource directory.
One last file we must look into before we complete this article is
It is from this file that the Android system invokes the MainActivity when the application is launched. Remember, there can be several activities in the same application. The ones that can be launched by the system are given a special category “android.intent.category.LAUNCHER“.
Others are custom or “android.intent.category.DEFAULT“. Such activities are typically invoked by one of the launched activities generally through intents which we will cover in later tutorials. That’s it for android studio tutorial, let me know through comments if you have any queries.