In programming, it is tradition for the first app of a new student to use whatever language/OS they are using to say hello to the world. In this tutorial, we will quickly build an app that does just that.
The complete code can be downloaded from https://github.com/PacktPublishing/Android-Programming-for-Beginners/tree/master/Chapter%2001.
Follow these steps to start the project.
1. Run Android Studio in the same way you run any other app. On Windows 10, for example, the launch icon appears in the start menu.
If you are prompted to Import Studio settings from…:, choose Do not import settings.
2. You will be greeted with the Android Studio welcome screen, as shown in the following screenshot. Locate the Start a new Android Studio project option and left-click it:
3. After this, Android Studio will bring up the New Project window. This is where we will perform the following:
- Name the new project
- Choose where on our computer the project files should go
- Provide a Company domain to distinguish our project from any others in case we should ever decide to publish it on the Play Store
4. The name of our project is going to be Hello World, and the location for the files will be your AndroidProjects folder.
5. The company domain can be almost anything you like. If you have a website, you could use the format Yourdomain.com. If not, feel free to use gamecodeschool.com, or something that you just make up yourself. It is only important when you come to publish.
6. To be clear, in case you can’t see the details in the following screenshot clearly, here are the values used. Remember that yours might vary depending upon your choices of company domain and project location:
Note that the application name has a space between “Hello” and “World,” but the project location does not and will not work if it does.
7. The following screenshot shows the New Project screen once you have entered all the information:
8. In the above screenshot, you can see that Android Studio has auto-generated a Package name based on the information entered. Mine is com.gamecodeschool.helloworld. Yours might be the same or it may differ; it doesn’t matter.
You can write Android apps in a few different languages, including C++ and Kotlin. There are various advantages and disadvantages to each compared to using Java. Learning Java will be a great introduction to other languages, and Java is also the official language of Android. Most top apps and games on the Play Store are written in Java.
9. Click the Next button, and then continue to configuring the Hello World project. The following set of options is the Target Android Devices window. You can leave the default options selected as you are only making apps for Phone and Tablet. The Minimum SDK option can be left as it is because it means the game will run on most (nearly all) Android devices, from Android 4.0 to the latest version.
10. This next screenshot shows the Target Android Devices window we have just discussed, mainly just for your reference:
11. Click the Next button.
12. The window that follows has a slightly obscure-sounding title, Add an Activity to Mobile. These are some useful project templates that Android Studio can generate for you depending on the type of app you are going to develop.
As a brief introduction, an Activity is a special class from the API and every Android app must have at least one. It is the part of the code in which your app will begin when it is launched by the user and handles interaction with the user. The options on this screen provide different ready-made templates of Activity class code to give programmers a fast start when creating various types of app. As we are starting from scratch, the most appropriate option for us is Basic Activity.
13. We will use the Basic Activity option. Android Studio will auto-generate a small amount of code and a selection of resources to get our project started.
14. Select Basic Activity. Here is a screenshot of the Add an Activity to Mobile tab with the Basic Activity option selected:
15. Make sure Basic Activity is selected and click Next.
16. On the Customize Activity screen, which you should now be looking at, we have a few changes to make. We could leave the defaults as they are, but then Android would generate more files than we need. In addition, we want to change the Activity Name to something more appropriate than MainActivity. Follow this short list of changes to configure the Customize Activity screen:
- Change Activity Name to HelloWorldActivity
- Notice that Layout Name changes automatically to activity_hello_world. Leave this as it is.
- Notice that Title changes automatically to HelloWorldActivity. Change it to My First Activity.
- Leave the Use a Fragment option unchecked.
17. Check that this is what the Customize Activity screen looks like when you’re done with the previous step:
18. Finally for this section, you can click the Finish button and explore a little of what you (and Android Studio) have just achieved.
Android Studio will prepare your new project. This might take a few seconds or a few minutes, depending on how powerful your PC is.
At this stage, you might be ready to proceed, but depending on the install process, you might need to click a couple of extra buttons.
Look at the bottom window of Android Studio to see if you have the following message:
Note that if you do not see a horizontal window at the bottom of Android Studio like the one shown below, you can skip these two extra steps.
Extra step 1
If you do see something like the above screenshot, click Install missing platform(s) and sync project, accept the license agreement, and then click Next, followed by Finish.
Extra step 2
If you get another message like this:
Click Install Build tools…. and then click Finish.
You can tidy up the screen a bit and close this bottom horizontal window by clicking the Messages tab on the very bottom of Android Studio, but this isn’t compulsory.
Hope you enjoyed reading this article. If you’d like to explore Android in more detail, you can check out, Android Programming for Beginners — Second Edition. A comprehensive and accessible guide, Android Programming for Beginners — Second Edition takes you through Android and Java programming with practical and actionable steps, so you can build in-depth, full-featured Android 9 Pie apps starting from scratch.