Android Adventures - Getting Started With Android Studio
Written by Mike James   
Tuesday, 21 January 2014
Article Index
Android Adventures - Getting Started With Android Studio
Project Structure
From Code to Emulator
Running the App

The Java

If you double click on the file you will see the code it contains. Some of the code might hidden but you can inspect it if you wan to by clicking the + buttons to expand it. The important part of the code is:

import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;

public class MainActivity extends ActionBarActivity {
protected void onCreate(Bundle savedInstanceState){

There are two other functions below the onCreate function but ignore these for the moment - they implement features you didn't really ask for - useful but not when you are just getting started.  

The onCreate function is the only thing that matters at the moment.

The onCreate function is called when your app is run and it is expected to create the view and do the whatever the Activity is concerned with.

As our Activity doesn't really do anything much the only thing onCreate has to do is first call the inherited OnCreate i.e super.onCreate to do all the standard things and then use the setContentView function to select the XML file that determines the layout of the Activities screen. The line:


is the most important. It gets the resource object that represents the layout as defined by the XML file created by the designer and makes it the current ContentView i.e. it is what is displayed on the screen. 

That is it makes the connection between the layout you have defined using the designer and the user interface that appears on the devices screen.

We have more to learn about the resource object R but you can see that its main role is to form a link between your Java code and the resources that have been created as XML files by the designer among others. 

As this is all our Activity does this is all the code we need.

While I agree it is hardly an "activity" it is enough to see the basic outline of an Android app and to see how to get it running - which is our next job.

Get Started with the Emulator 

There are two distinct ways of running an Android app using Android Studio. You can use the emulator or a real Android device. Eventually you will have to discover how to run an app on a real connected Android device because the emulator only allows you to test a subset of things. 

However for the moment running your first app on an emulator is quite enough to get started. 

Before you try to run the program you need to configure an Android Virtual Device AVD. You can essentially create as many AVDs as you like each with a different set of characteristics corresponding to different hardware.

All you need to do is use the menu command: 

Tools,Android, AVD Manager.

and wait while the AVD Manager loads. 




The simplest way to get an AVD ready to use is to select the Device Definitions tab and scroll down until you find the device you want to use for the test. Click to select it and then click the Create AVD and you will be given the opportunity to customize the resources that the device has. 




Now that you have an AVD simply select it in the Android Virtual Devices tab and click the Start button.

It usually takes quite a time for the AVD to start because it not only has to initialize the emulator it also has to boot Android just like a real device. However you only have to do this once per debug session because the AVD can be reused and the app reloaded each time you make any changes and this is quite a quick operation. Most Android programmers get into the habit of starting an AVD when they first start using Android Studio so that it is ready for testing when the program is. 

Depending on the OS you are developing on you may have to reduce the amount of memory used by AVD  - Windows cannot work with more than 768Mb. You may also find that the emulator runs faster if you select the Use Host GPU option. 

Finally remember to wait until the Android operating system is loaded and you see the familiar home screen before you try to run your app and spend time wondering why it hasn't worked. 



Last Updated ( Thursday, 20 November 2014 )

Copyright © 2015 All Rights Reserved.
Joomla! is Free Software released under the GNU/GPL License.