Introduction To Android Development With Android Studio

Ellis Wright
7 min readAug 9, 2018

--

Application development has quickly become one of the most popular and often lucrative forms of development. Here is the best way to get your Android development environment up and running in just a few minutes.

Installing Android Studio

In order to start programming for Android you will first need to install Android Studio IDE, this will allow for easy programming and testing of your app. Alternatively you can use Eclipse IDE, however the installation is more complicated and this tutorial will be focused on Android Studio; you can find instructions for installing Eclipse for Android development here.

Windows:

Simply run the installer and choose the express installation option within the program. It is a large application and will take a while to fully install depending on your computer’s specs. Once the installer has completed you should be able to run Android Studio from your desktop or the applications menu.

Linux:

On Linux the installation is only slightly more complicated; the download will be in the form of a compressed zip folder. Start by extracting the contents of the zip folder to where you want to install Android Studio, then open a terminal window and set your current working directory to the bin folder within the extracted directory either with cd ../android_studio/bin/ or by right clicking in the folder and selecting open in terminal if you have open-terminal installed. Then simply run the studio.sh file with the command ./studio.sh. The installer will then run; choose express settings and wait for it to finish. Once installed you will be able to run Android Studio by navigating to the same bin folder and running the ./studio.sh file once again.

Mac:

Simply run the installer, and choose the express installation method within the wizard. Once the installation is complete you will be able to launch Android Studio from the applications menu.

Choosing An SDK/API Version:

Once you have launched Android Studio you will be given a welcome screen asking if you want to create a new Android project, or load a previous one. Click to start a new project and give it a name. Here I call mine Android tutorial, you can set the “company domain” to your person website or your apps website, otherwise leave the default.

Then click next to get to the all important Target Android Devices Screen. Here you can choose what version of Android you are making an app for (phone, tv, android wear) and which API version is your minimum. For now leave only the Phone and Tablet box checked, you can experiment with android tv and android wear applications later.

Choosing your minimum API version is a very important step in creating your application. When older phones lose support by their manufacturers they are stuck on older versions of Android, this means that they can’t take advantage of newer features in many apps. Choosing which version to be your minimum is about striking a balance between the percentage of phones who will be able to run your app (which reduces as the minimum version increases) and the robustness of the features available in that API version. So choosing API 15 as your minimum may allow 100% of Android devices to run the app, but you will lose many of the features that come in later updates.

In this tutorial I will use API 21 as the minimum, covering around 70% of devices. However, for your app you will need to research what features you will need and what kind of audience you want to reach before making this decision. You can find more information about the differences between each API version here.

After clicking next you will be taken to an Activity selection screen. For the purposes of this tutorial choose “Empty Activity” and click next to give your activity a name. Congratulations you have now set up our first Android Application! Click finish and Android Studio will spend some time setting up your development environment, be patient as this could take a while.

Hello World! (The App)

After going through the process of setting up Android Studio it is now time to create your very first application. We will start with a simple version of the classic “Hello World!” program, which will show the text “Hello World!” on screen when a button is pressed. Start by expanding the project structure on the left hand side of AS (Android Studio). Within the res.layout package you should see an xml file, this is the “layout” for your activity.

The layout defines what is shown to the user, and how it is represented. Phones can have varying shapes, sizes, and resolutions, so most of the objects you place on the layout (buttons, text fields, etc…) will be located relative to each other rather than having a certain pixel distance of padding in between them like some of you may be used too in CSS. When you open the layout you should see a representation of what the application will look like on the right side, and a list of widgets and other tools on the left. You can add or remove objects from the layout by dragging and dropping them.

Creating the Layout

To Start our App we will place a Button and a TextField onto the layout. When you click on each item a list of the items attributes will appear on the right side of the screen. This allows you to change the text of the Button or TextField as well as many other useful items. For now let’s set the TextField’s text attribute to nothing, and its id attribute to helloText. For the Button the text attribute can be set to “Click Me!” and its id attribute to helloButton.

You are almost done with the layout; the last thing we need to do is make sure our two items are constrained so they display properly on the app when it runs. To do this you can either have AS automatically infer the constraints by clicking the wand icon at the top of the layout, or by manually creating the constraints by drawing connections between the open circles of each object.

Programming the App

In order to add functionality to your app you need to edit the code behind the layout, to do this open up the .java file which corresponds to the layout we just created (it should be named something similar to the layout xml file).

In order to get our button working you will need to add an event handler for its onClick event. Go into the onCreate method, which runs when the app is initialized, and add some code. First we need to initialize variables for the Button and the TextView.

These lines of code retrieve the elements from the layout by their id which you assigned in the previous section and set them to be variables you can use within your code.

Next you need to add the event handler to the button, the final version of the class should have an onCreate method resembling this:

Running your Application

The only thing left to do is run your application through an android virtual device. When you click the green play icon in the top right of AS it will present you with a blank screen which has the option for you to create a new Android Virtual Device. You will have to download the latest version of the SDK you want to use, give your virtual device a name, and choose which device to emulate. Alternatively you can connect your own android device and run your app through your phone.

Then select the device you just created, or your connected device, and click “okay”. Your app should automatically open once the virtual device is finished running. Congratulations! You just created your first Android application!

Originally published at thetechbubble.net on August 9, 2018.

--

--