Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Android Programming for Beginners

You're reading from   Android Programming for Beginners Build in-depth, full-featured Android 9 Pie apps starting from zero programming experience

Arrow left icon
Product type Paperback
Published in Oct 2018
Publisher Packt
ISBN-13 9781789538502
Length 766 pages
Edition 2nd Edition
Languages
Tools
Arrow right icon
Author (1):
Arrow left icon
John Horton John Horton
Author Profile Icon John Horton
John Horton
Arrow right icon
View More author details
Toc

Table of Contents (33) Chapters Close

Preface 1. Beginning Android and Java FREE CHAPTER 2. First Contact – Java, XML, and the UI Designer 3. Exploring Android Studio and the Project Structure 4. Getting Started with Layouts and Material Design 5. Beautiful Layouts with CardView and ScrollView 6. The Android Lifecycle 7. Java Variables, Operators, and Expressions 8. Java Decisions and Loops 9. Java Methods 10. Object-Oriented programming 11. More Object-Oriented Programming 12. The Stack, the Heap, and the Garbage Collector 13. Anonymous Classes – Bringing Android Widgets to Life 14. Android Dialog Windows 15. Arrays, ArrayList, Map and Random Numbers 16. Adapters and Recyclers 17. Data Persistence and Sharing 18. Localization 19. Animations and Interpolations 20. Drawing Graphics 21. Threads, and Starting the Live Drawing App 22. Particle Systems and Handling Screen Touches 23. Supporting Different Versions of Android, Sound Effects, and the Spinner Widget 24. Design Patterns, Multiple Layouts, and Fragments 25. Advanced UI with Paging and Swiping 26. Advanced UI with Navigation Drawer and Fragment 27. Android Databases 28. Coding a Snake Game Using Everything We Have Learned So Far 29. Enumerations and Finishing the Snake Game 30. A Quick Chat Before You Go Other Books You May Enjoy Index

Our first Android app

Now we can get started on the first app. 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. We will quickly build an app that does just that, and in Chapter 2, First Contact – Java, XML, and the UI Designer, we will go beyond that and add some buttons that respond to the user when they are pressed.

Note

The complete code as it stands at the end of this chapter is in the download bundle in the Chapter 1 folder for your reference. You can't simply copy and paste this code, however! You still need to go through the project creation phase explained in this chapter (and at the beginning of all projects), as Android Studio does lots of work behind the scenes. Once you become familiar with these steps and understand which code is typed by you, the programmer, and which code/files are generated by Android Studio, you will then be able to save time and typing by copying and pasting the files I supply in the download bundle.

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.

    Tip

    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:
    Our first Android app
  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 that we created in the Setting up Android Studio section.
  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 I used. Remember that yours might vary depending upon your choices of company domain and project location:

    Option

    Value entered

    Application name:

    Hello World

    Company domain:

    gamecodeschool.com

    Include C++ support

    Leave this option unchecked (see the next information box if you want to know more)

    Project location:

    D:\AndroidProjects\HelloWorld

    Tip

    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.

    The following screenshot shows the New Project screen once you have entered all the information:

    Our first Android app
  7. In the previous 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.

    Note

    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.

  8. Click the Next button, and then we will continue to configure the Hello World project. The following set of options is the Target Android Devices window. We can leave the default options selected as we are only making apps for Phone and Tablet. The Minimum SDK option can be left as it is because it means our game will run on most (nearly all) Android devices, from Android 4.0 to the latest version.

    Note

    We already know that the Android SDK is the collection of packages of code that we will be using to develop our apps. Like any good SDK, the Android SDK is regularly updated, and each time it gets a significant update the version number is increased. Simply put, the higher the version number, the newer the features you get to use; the lower the version number, the more devices our app will work on. For now, the default API 15, Android 4.0.3 (Ice Cream Sandwich), will give us lots of great features and near 100% compatibility with the Android devices currently in use. If, at the time of reading, Android Studio is suggesting a newer API, then go with that.

    If you are reading this some years in the future, then the Minimum SDK option will probably default to something different, but the code in this book will still work.

  9. This next screenshot shows the Target Android Devices window we have just discussed, mainly just for your reference:
    Our first Android app
  10. Click the Next button, and then we will move on.
  11. The window that follows has the 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. We will learn all about Android Activity as the book progresses.

    Note

    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 our 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.

  12. 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. We will discuss the code and the resources in detail in the next chapter.
  13. Select Basic Activity. Here is a screenshot of the Add an Activity to Mobile tab with the Basic Activity option selected:
    Our first Android app
  14. Make sure Basic Activity is selected and click Next.
  15. 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. We will explore the significance of this in the next chapter.
    • Notice that Title changes automatically to HelloWorldActivity. Change it to My First Activity.
    • Leave the Use a Fragment option unchecked. We will explore Android Fragments later in the book.
  16. Check that this is what the Customize Activity screen looks like when you're done with the previous step:
    Our first Android app
  17. Finally for this section, you can click the Finish button and we will explore a little of what we (and Android Studio) have just achieved.

Android Studio will prepare our new project for us. This might take a few seconds or a few minutes, depending upon 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.

Tip

This is why I mentioned that we are "probably" finished installing and setting up.

Look in the bottom window of Android Studio to see if you have the following message:

Note

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

Extra step 1

If you do, 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:

Extra step 2

Click Install Build tools…. and then click Finish.

Tip

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.

You have been reading a chapter from
Android Programming for Beginners - Second Edition
Published in: Oct 2018
Publisher: Packt
ISBN-13: 9781789538502
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €18.99/month. Cancel anytime