Your first Android app in Android Studio

STEP EIGHT - RUN THE APP

Develop Highly Valued Skills

Having learnt a diverse range of programming languages during my computer science degree, it was exploiting these skills in personal projects that gave me an advantage while searching for employment; these projects were focused on mobile apps and games.

The explosive shift from static desktop devices to portable mobile devices has not only opened up huge potential markets for mobile developers, but reduced development costs by providing a surprisingly unified environment – 95% of mobile devices falling into the category of running Android or iOS. The rise in popularity of the mobile app, a program fulfilling a single well-defined purpose, further compounds falling development costs since it is entirely possible for a single developer to create these fragments of functionality.

Continue reading to begin your mobile development journey, with a step-by-step guide to create your first Android app in Android Studio.

Why Android?

Android currently has lower barriers to entry for developers than iOS: there is a small one-off payment ($25) required if you intend to place apps on the Google PlayStore, however this is optional, and so long as you have a PC or laptop, development is otherwise entirely free. By comparison, iOS development requires a Mac computer and an annual developers license ($99). Google also has relaxed constraints on apps submitted to the PlayStore, whereas iOS AppStore apps must pass fairly stringent validation before acceptance.

Some Advice…

The first time I ever attempted to make an Android app, I reached for the Android SDK and Eclipse IDE, and decided I would make a quiz game. Opening Eclipse, I was completely bowled over, since I had no experience of Java, the Android API, or a project with a GUI. In hindsight, rather than battling on by following numerous tutorials advocating a copy-and-paste learning style, I would have divided and conquered the above-mentioned challenges sequentially, which is what I ended up doing in any case.

If you have little or no experience coding in Java, it would be well worth your while following a course to become familiar with its syntax before applying it to Android development: in my case I religiously followed the free “Practice It Java” online resource provided by the University of Washington, harbouring a wealth of interactive programming problems to be solved in Java.

Android development is more than just writing Java, but having first conquered the language, learning to work with the API is significantly easier. Working with a decent IDE provides you with auto-completion and templates, which reduces the amount of the API that you must hold in your memory at any one time. As a staunch fan of the IntelliJ IDEA Java IDE, I was particularly taken by the modified version of IntelliJ optimized for Android development – Android Studio – currently developed directly by Google. Having now amassed hundreds of hours of experience with Android Studio, it is remarkably stable for a program that remains in prolonged beta, provides many timesaving features, such as intelligent text prediction and safe renaming..

There are huge swathes of material for learning the Android API and writing XML layout files, whether it be YouTube tutorials, massive open online courses such as Udacity, tutorial websites such as Vogella, not to forget the Android Developer training provided by Google. And don’t forget that on StackOverflow your question has almost certainly already been answered, with several correct answers.

Get Started!

Making your first Android app is as simple as modifying the hello world app to contain your own content (as demonstrated below), but ultimately my advice would be to take a step back, invest some time into the skills forming the foundations of Android development, then working on some achievable projects – it may well end up paying your bills for years to come!


TUTORIAL: MAKE YOUR FIRST ANDROID APP IN ANDROID STUDIO

STEP 1 – DOWNLOADING THE IDE

First, you will need to download and install Android Studio IDE from http://developer.android.com/sdk/index.html for the following steps.

STEP ONE - DOWNLOADING THE IDE

STEP 2 – DOWNLOADING THE SDK

Next up, open the SDK manager from within Android Studio at Tools > Android > SDK Manager and download the latest API level, Platform Tools and Build Tools. These are usually pre-selected – you just need to click install packages and agree to the license agreements.

STEP TWO - DOWNLOADING THE SDK

STEP 3 – CREATE A NEW ANDROID PROJECT

Click New Project, name your project, select the default minimum API level, then select Blank Activity, leaving all the file names as default.

STEP THREE - CREATE A NEW PROJECT

STEP 4 – MODIFY THE MAIN LAYOUT FILE

At this point, the number of windows filling the IDE may blow you away, but we will take it step by simple step to see what they are doing. On the left hand side is the project file explorer, then in the main coding window is a rendered view of the default layout file, with a palette to add new layout components to its left and some details about the file’s contents to the right. For this example, find the Button item under Widgets in the Palette window, and drag it onto the screen of the phone in the middle: this button will act as a bridge into our custom activity. If you click on the Text tab at the bottom (as opposed to the Design tab), you will see that there is now a Button component written in the XML.

STEP FOUR - MODIFY THE LAYOUT FILE

STEP 5 – MAKE A NEW ACTIVITY

Browse to java > your.package.name in the project file explorer, then right click on the package. Having found this, create a new blank activity by clicking New > Activity > BlankActivity, and name this “CustomActivity” (or whatever takes you!), then click finish. This will have created a new java code file, which governs the behavior of the activity, and also new XML layout file, which governs what is displayed.

STEP FIVE - MAKE A NEW ACTIVITY

STEP 6 – MODIFY THE CUSTOM ACTIVITY XML FILE

Browse to res > layout in the project explorer, then double click to open the activity_custom.xml layout file. Switch the tab from Design to Text, then alter the value of the TextView attribute ‘text’ from “Hello world!” to “My first android app!”. This text will be displayed when we open up our custom activity by clicking on the button we made previously.

STEP SIX - MODIFY THE CUSTOM XML FILE

STEP 7 – WIRING UP THE MAIN ACTIVITY BUTTON

Open up the MainActivity java file, and add the following line to the onCreate method: Button nextActivityButton = (Button) findViewById(R.id.button);

This will find the component with an id of button in the layout file, and provide us with a reference to it so that we can manipulate its behavior. The auto-complete feature of Android Studio can come into its own now, as we want to create an OnClickListener to determine what occurs when the Button is clicked. Write n and press tab – nextActivityButton should now be written. Then write ‘.’ and then tab – setOnClickListener should now be written. Then write ‘new O’ and press tab and a new OnClickListener template should be auto-completed. All that is left to do now is writing the API call that will tell Android to display our custom activity, so add the following line inside the onClick method: startActivity(new Intent(MainActivity.this, CustomActivity.class));

STEP SEVEN - WIRING UP THE BUTTON

STEP 8 – RUN THE APP IN THE EMULATOR OR ANDROID DEVICE

Press the green play button on the top bar of the IDE, and wait for the ‘Choose Device’ popup – you can choose an Android device you have plugged in with USB Debug Mode enabled, or start the app in an emulator.

STEP EIGHT - RUN THE APP


DOWNLOAD THE SOURCE CODE


KEYWORDS: android, mobile, developer, programming, apps, android studio

rowanstringer

I am a software engineer with two years of industry experience, developing commercial systems for the retail industry. I have worked with a diverse range of programming languages ranging from C++, C# and Java, to scripting languages Python, Bash, JavaScript, Razor MVC and Powershell. I’ve worked on a variety of applications for mobile, servers and embedded devices. In my spare time, I create and play video games, learn foreign languages and breed honey bees.