Install Android Studio

Download Android Studio (current version at time of writing is 3.2) from https://developer.android.com/studio/ and install on your computer. Versions are available for Windows, Mac and Linux. Only the latest SDK will be installed by default, which is currently Android 9.0 (Pie) API level 28. I intend to develop for my Samsung Galaxy Tab A which has Android 7.1.1 installed, so I should install that in Android Studio. Click on the Configure button at the bottom of the splash screen.

Then SDK Manager, check any SDK version you think you might need, and then click OK. The SDKs will be installed for you. When you create an app you can specify the earliest version of Android that must be supported.

Create New Project

Click the Start a new Android Studio project link on the splash screen and the Create Android Project dialogue opens up. There’s a long tradition in Programming circles to have one’s first application display the message Hello World, and in fact that is the default output of a new project in Android Studio without your making any changes, so we’ll go with that.

In the Project location field Android Studio fills in a directory based on a default location (I think that’s in your documents folder) plus a directory name derived from the Application name. Android Studio does not allow spaces in directory paths, so if there is a space in the application name it is simply removed in the directory name. You can use the button with … to select a different location if you want.

Packages are a way of organizing files – they are equivalent to subdirectories – and Android Studio uses the domain name and app name to construct a package name and create the corresponding subdirectories within your main project directory. I have used a domain name that I own, and if you own a domain name I suggest you put it in here too. Else make up something that should be unique to you, although if you’re not publishing your app then it doesn’t matter too much, and my.projects or something similar will work just fine.

I will be focusing on Java in this series of tutorials, so won’t check the boxes for C++ and Kotlin support.

The Next screen asks about the devices you are developing for. I have just selected Phone and Tablets, and selected API 24 which is what my tablet runs (actually it runs a slightly later API but no matter).

Next select the minimum SDK that you want for your app. I’ve chosen Android 7.0 (API 24) for my Samsung tablet (I checked the tablet specs to see what version of Android it was using), however if you want your app to run on more devices choose an earlier version. The screen tells you how many devices will be able to run your app, so that’s a handy guide. Also, I’m not looking to run this first app on anything other than a phone or tablet.

An activity is a unit of functionality in Android. Generally one activity is associated with one screen layout, and when you create an activity you get a Java code file and an XML layout file created. The next screen has a few presets – select the Empty Activity for this first simple app. It’s not actually completely empty, you do get the Hello World text message.

The Configure Activity dialogue opens with the default name of MainActivity. You should check the Generate layout file checkbox to get the XML file. I’m not sure what the Backwards compatible checkbox actually does. The app is supported on the earliest device that you specified previously even if you don’t check this box. I think it’s got more to do with look and feel than actual functionality.

Explore the File Structure

The next screen shows the Project explorer (file browser) on the left, with the two files that were created in their appropriate places in the directory tree. By default the two files are open in separate tabs in the code editor pane on the right. On line 11 in the Java class MainActivity you can see that it sets the layout by referring to the activity_main XML layout file.

As a matter of coding style I like to line up (vertically) matching pairs of braces {} so I’ve slightly edited the file to add a couple of line feeds (enter key) to achieve this. It makes no difference to how the code works.

I’ll be going into a lot more detail about Java classes in later tutorials. For this first exercise we are simply going through the process of creating a simple app and then deploying it to an actual device, so don’t sweat too much about what this code is actually doing yet.

If you click on the activity_main.xml tab in the code editor, you can select (at the bottom of the pane) either the design view or the text view of the file. Android Studio has a drag and drop design editor, where you drag components from the Palette on the left side of the design view right onto the device. In this default layout there is a layout component and a TextView component which has the message Hello World that we’ve mentioned before. You can select and delete that TextView if you want, but I suggest you leave it to see how it appears on your device (or virtual device if you don’t have a real Android device).

At the top of the Design view you will see Nexus 4 displayed in a drop-down list. This is the default device used in Design view, but you can click on that and select a different preset if you wish. However this is just the appearance in the design view. We need a virtual device in the emulator to get a better look at our app.

Run in the Emulator

From the Run menu select Run app (or click the green arrow in the toolbar below it). A window opens asking you to select a device. You probably won’t have any so click on the Create new virtual device button at the bottom, then pick from the list of presets on the next screen, and the Android version on the one after that. Then you select that device in the first screen and click OK. It can take a while for a virtual device to initialize, but if all goes well the device will appear on your screen and you can interact with it the way you would interact with a real one. In this screenshot I’ve used the Nexus 4.

Deploy to Real Device

To deploy the app to an actual device we need to build the APK file. Do this by clicking on the Build menu at the top, then Build bundle(s)/apk(s), then build apk. At the top of the Project Explorer click on Android and select Project instead from the drop-down. Your apk file will be found in app/build/output/apk/debug and is called app-debug.apk by default.

Copy this file (Ctrl-C). Connect your device to your PC. I use the cable that came with my Samsung tablet. Your device should appear in your PC file browser, so I just paste the file into the root directory (Tablet on my device).

Then I run the File Manager on my tables (My Files), click on the file, and a dialogue opens asking if I want to install it. Answer yes, and then you can run it on your device. An icon should appear on your home window.

A journey of 1000 apps begins with Hello World! Happy apping!

8 votes, average: 4.75 out of 58 votes, average: 4.75 out of 58 votes, average: 4.75 out of 58 votes, average: 4.75 out of 58 votes, average: 4.75 out of 5 (8 votes, average: 4.75 out of 5)
You need to be a registered member to rate this.
(283 total tokens earned)
Loading...

Responses