In programming of any kind, it is traditional to create a Hello World app for your first project. This simply means you’re printing the words ‘Hello World’ to the screen, which requires a very basic understanding of how to display text and more importantly, how to create and run a basic program.
Android Studio makes things very easy in the sense that as soon as you create a new project, it will be populated with the very basic code and set-up necessary to print ‘Hello World’ to the screen. The tricky part in this case is actually running your app!
Reverse engineering an app
Android Studio has built the Hello World app for us then, but we should still have a basic understanding of how it’s done.
So, remember: the MainActivity.java is the code that will run as soon as your app is launched (which is defined in the AndroidManifest.xml, in case you ever wanted to change it). The line we’re interested in is:
This tells Android to show the activity_main.xml file on the screen. It’s inside a ‘method’ called onCreate and any code inside there will run as soon as the app starts up. We’ll discuss this more in a future post.
Click the ‘activity_main.xml’ tab in order to take a look at the layout. There are two different views that area available for layout files, the ‘Design’ view and the ‘Text’ view. You can swap between these by finding the tabs at the bottom of the window and clicking on the relevant option.
The Text view shows you the code. This is code in the sense that HTML is code: it can’t handle logic etc. but is simply used to describe elements on the screen. It might also have a preview available on the right, depending on the size of your monitor.
Open up the Design view though and you can see a full-screen preview of your activity. Seeing as we only have one activity and it is completely static, this is a preview of your app for all extents and purposes. And as you can see, it says ‘Hello World!’.
Double click on that text and you’ll see a window open up on your right. This text is actually added to something called a ‘View’ and the specific type of view in this case is ‘TextView’. As the name rather implies, the TextView is a view that shows text on the screen. Other examples of views include buttons, labels, lists and more and we’ll see how to work with these in subsequent posts.
Notice that in that right hand window are a number of boxes with different attributes. The ‘text’ box contains the words ‘Hello World!’ and if you were to change that, you could change the text showing on the screen. Nice and simple!
Likewise, if you were to go into the Text view and change the line that reads:
Then you could similarly change the text being displayed. Any changes you make in the Text view will be reflected in the Design view and vice versa. It’s also possible to drag the view around the screen and to make it larger with the mouse.
Running the app: an introduction to virtual devices
Like I said, creating a Hello World app is the easy part with Android. The more complex part is running it. And of course, the difficulty here comes from the fact that we’ve developing using a different machine from the one we’re targeting. We’re building an app for Android devices but we’re using a Windows device (most likely).
That means we have two options when it comes to testing apps:
- Run the app on a phone or tablet connected via USB
- Run the app on an emulator
Anyone who has ever played Sonic or Mario on their laptop or smartphone will know what an emulator is. This is a program that allows one operating system to run applications designed for another. The good news is that Android Studio comes with emulators packaged-in via the AVD Manager or ‘Android Virtual Device’ Manager.
Testing on a Virtual Device
To get started, choose Tools > Android > AVD Manager from the menu up-top.
(See how we’re starting to get familiar with these menu options? As much as Android Studio has become a lot more welcoming for beginners, it is still pretty obtuse. How would someone starting out on their own know what an AVD Manager is? Fortunately, if you take each challenge as it comes, you can familiarize yourself everything gradually.)
You have to build your Android virtual devices yourself. This allows you to create devices with different specifications, screen sizes etc. and thereby make sure that your app is compatible with as many phones and tablets as possible. Seeing as you haven’t built any yet, there is nothing here right now; just an option to ‘Create Virtual Device’.
Hit that and you will be greeted with a screen where you can choose the hardware you want to use. This is specifically to define the screen size and resolution and the default selection is currently a Nexus 5X. You can leave it as that and try adding more devices in future when you want to see how your apps look on bigger or smaller screens. Click Next and you can then choose the Android version you want to use.
You’ll need the corresponding system image, which If you don’t have this installed, then you will be prompted to select ‘Install’ and then ‘Accept’ and it will start to install for you. Once that’s done, you can select that system image and you’ll then be taken to a final screen where you can choose some more configuration settings. This lets you decide how much memory you want to allocate to your emulator etc. You can leave all this as it is for now and then just click ‘Finish’.
Now if you click the little green play icon at the top of Android Studio, or if you click Run > Run ‘app’ (no, I don’t get the air quotes either), then you’ll be given the option to select your virtual device. If everything has gone according to plan, then after a significant amount of loading, you should be greeted with your app and the words ‘Hello World!’ staring you in the face. Congratulations: that’s your first working application!
As a fun aside, you can actually use this Android Emulator just like any other Android device. Click ‘Home’ and you can exit the app you’ve built and then do anything you would with your phone. Why not go to the Play Store and install some of your apps? You can use your Google credentials as normal!
Note that emulation may be pretty slow depending on your hardware. If you have a modern, powerful machine then you will be able to run in an accelerated mode which is actually suitable for gaming. Otherwise, you will find performance to be pretty slow going and it might take a long time to boot up for the first time.
Testing on your phone
Seeing as the emulator requires some beefy hardware and a bit of patience, you may opt instead to try testing your apps on your physical device. If you have a phone or a tablet to hand, then you can simply plug that in and hit ‘Play’.
Well, almost. There are two things you need to do first. The first is that you need to head into your settings and find the option to allow apps to be installed from ‘unknown sources’. This will likely be in the security section and all it is doing is telling your device that it’s okay to install apps that aren’t downloaded from the Play Store.
You’ll also need to enable an option called ‘USB Debugging’. This can likely be found in your ‘Developer Options’ which in turn might be hidden away. The process used to access these settings varies from one device to the next, so the best option is to do a quick Google search to find out how to access them on yours.
Of course, you’ll also need to ensure you have the correct drivers installed for your device and that it is unlocked and on. But if you’ve been using your phone to transfer files, then you should be ready to go.
Plug it in, hit play and then watch as your first app boots up on your phone. That’s a win right there!
So, it has taken us four posts to get to the point where you have an app showing on your screen. You might be feeling a little exhausted at this point.
But don’t be disheartened. Android Studio is a complicated tool and getting it all set up is a difficult process. Now that’s done though, you’ll also find it’s actually a very powerful and intuitive piece of software too. For a while then, things are about to get a whole lot easier as you start adding views and making them do cool things, all with minimal code needed.
Then it gets a lot, lot harder again. But let’s focus on the easy stuff for now.