You are previewing Android Programming for Beginners.
O'Reilly logo
Android Programming for Beginners

Book Description

Learn all the Java and Android skills you need to start making powerful mobile applications

About This Book

  • Kick-start your Android programming career, or just have fun publishing apps to the Google Play marketplace

  • A first-principles introduction to Java, via Android, which means you’ll be able to start building your own applications from scratch

  • Learn by example and build three real-world apps and over 40 mini apps throughout the book

  • Who This Book Is For

    Are you trying to start a career in programming, but haven’t found the right way in? Do you have a great idea for an app, but don’t know how to make it a reality? Or maybe you’re just frustrated that “to learn Android, you must know java.” If so, Android Programming for Beginners is for you. You don’t need any programming experience to follow along with this book, just a computer and a sense of adventure.

    What You Will Learn

  • Master the fundamentals of coding Java for Android

  • Install and set up your Android development environment

  • Build functional user interfaces with the Android Studio visual designer

  • Add user interaction, data captures, sound, and animation to your apps

  • Manage your apps’ data using the built-in Android SQLite database

  • Find out about the design patterns used by professionals to make top-grade applications

  • Build, deploy, and publish real Android applications to the Google Play marketplace

  • In Detail

    Android is the most popular OS in the world. There are millions of devices accessing tens of thousands of applications. It is many people's entry point into the world of technology; it is an operating system for everyone. Despite this, the entry-fee to actually make Android applications is usually a computer science degree, or five years’ worth of Java experience.

    Android Programming for Beginners will be your companion to create Android applications from scratch—whether you’re looking to start your programming career, make an application for work, be reintroduced to mobile development, or are just looking to program for fun. We will introduce you to all the fundamental concepts of programming in an Android context, from the Java basics to working with the Android API. All examples are created from within Android Studio, the official Android development environment that helps supercharge your application development process.

    After this crash-course, we’ll dive deeper into Android programming and you’ll learn how to create applications with a professional-standard UI through fragments, make location-aware apps with Google Maps integration, and store your user’s data with SQLite. In addition, you’ll see how to make your apps multilingual, capture images from a device’s camera, and work with graphics, sound, and animations too.

    By the end of this book, you’ll be ready to start building your own custom applications in Android and Java.

    Style and approach

    With more than 40 mini apps to code and run, Android Programming for Beginners is a hands-on guide to learning Android and Java. Each example application demonstrates a different aspect of Android programming. Alongside these mini apps, we push your abilities by building three larger applications to demonstrate Android application development in context.

    Downloading the example code for this book. You can download the example code files for all Packt books you have purchased from your account at If you purchased this book elsewhere, you can visit and register to have the code file.

    Table of Contents

    1. Android Programming for Beginners
      1. Table of Contents
      2. Android Programming for Beginners
      3. Credits
      4. About the Author
      5. About the Reviewers
        1. Support files, eBooks, discount offers, and more
          1. Why subscribe?
          2. Free access for Packt account holders
      7. Preface
        1. Why Android?
          1. But is this book for me?
        2. What this book covers
        3. What you need for this book
          1. Windows
          2. Mac OS X
          3. Linux
        4. Who this book is for
        5. Conventions
        6. Reader feedback
        7. Customer support
          1. Downloading the example code
          2. Downloading the color images of this book
          3. Errata
          4. Piracy
          5. Questions
      8. 1. The First App
        1. How Java and Android work together
          1. The Android API
          2. Java is object-oriented
          3. What exactly is Android?
        2. The development environment
          1. A note to the existing developers who use Eclipse
          2. The JDK
            1. What if I already have Java?
            2. Installing the JDK
          3. Setting up Android Studio
        3. What makes an Android app
          1. Android resources
          2. The structure of Android's Java code
            1. Android packages
        4. Our first Android app
          1. Creating the project
            1. A note on version controlling
          2. Deploying and testing the app
            1. Deploying to an emulator
            2. Deploying to a real Android device
        5. FAQ
        6. Summary
      9. 2. Java – First Contact
        1. Examining the log output
          1. Exploring the project assets and code
            1. Examining the layout file
        2. Improving our app and deploying again
          1. Modifying the UI
        3. Meet Java
          1. Java comments
            1. Sending messages
          2. Writing our first Java code
            1. Writing our own Java methods
            2. Examining the log output
        4. FAQ
        5. Summary
      10. 3. Exploring Android Studio
        1. The Android Studio guided tour
          1. Parts of the UI
            1. The console
            2. More console features
            3. The project explorer
          2. Transforming the editor into a design studio
            1. E1 – the Preview toolbar
            2. E2 – exploring the palette
            3. E3 – the layout preview
            4. E4 – the Component Tree
            5. E5 – the Properties window
            6. E6 – text and design view tabs
          3. The project folder and file structure
        2. FAQ
        3. Summary
      11. 4. Designing Layouts
        1. Exploring Android UI design
        2. Structure of a UI design
          1. Configuring and using widgets
            1. Widget properties
              1. Setting the size
                1. Sizing using dp
                2. Sizing fonts using sp
                3. Determining size with wrap or match
              2. Using padding and margin
              3. Using the layout_weight property
              4. Using gravity
              5. More properties
            2. Experimenting with widgets
          2. Containing widgets in layouts
            1. RelativeLayout
              1. RelativeLayout in action
            2. Using LinearLayout
        3. Summary
      12. 5. Real-World Layouts
        1. Building a real-world UI
          1. Creating a tablet emulator
          2. List-detail layout with ScrollView and LinearLayout
          3. Designing a form with RelativeLayout
        2. Summary
      13. 6. The Life and Times of an Android App
        1. Introduction to the Android lifecycle
        2. A simplified explanation of the Android lifecycle
        3. Lifecycle phases – what we need to know
        4. Lifecycle phases – what we need to do
        5. The lifecycle demonstration app
          1. Some other overridden methods
        6. The structure of Java code – revisited
        7. Summary
      14. 7. Coding in Java Part 1 – Variables, Decisions, and Loops
        1. Java is everywhere
        2. Syntax and jargon
        3. More code comments
        4. Storing and using data with variables
          1. Types of variables
          2. Primitive types
          3. Reference types
          4. Variable declaration
          5. Variable initialization
        5. Changing values in variables with operators
        6. Expressing yourself demo app
        7. Decisions
          1. Indenting our code
          2. More operators
          3. If they come over the bridge, shoot them
          4. Else do this instead
          5. Switching to make decisions
          6. The Switch Demo app
        8. Repeating code with loops
          1. While loops
            1. Breaking out of a loop
            2. Continue
          2. Do while loops
          3. For loops
          4. Loops demo app
        9. Summary
      15. 8. Coding in Java Part 2 – Methods
        1. Methods revisited
          1. The method structure
            1. Modifiers
            2. Return types
            3. The name of a method
            4. Parameters
            5. Working in the method body
          2. Using methods – demo apps
            1. Real-world methods
            2. Exploring method overloading
        2. Scope and variables revisited
        3. FAQ
        4. Further reading
        5. Summary
      16. 9. Object-Oriented Programming
        1. Important memory management warning
        2. Introducing OOP
          1. What is OOP exactly?
            1. Encapsulation
            2. Polymorphism
            3. Inheritance
          2. Why do it like this?
          3. A class recap
        3. Taking a look at the code for a class
          1. The class implementation
          2. Declaring, initializing, and using an object of the class
        4. The basic classes mini app
          1. More things we can do with our first class
        5. Remember that encapsulation thing?
          1. Controlling class use with access modifiers
            1. Class access in a nutshell
          2. Controlling variable use with access modifiers
            1. Variable access summary
          3. Methods have access modifiers too
            1. A method access summary
          4. Accessing private variables with getters and setters
          5. Setting up our objects with constructors
          6. Static methods
        6. Encapsulation and static methods mini app
        7. OOP and inheritance
        8. The inheritance mini app
        9. Polymorphism
          1. Abstract classes
          2. Interfaces
        10. FAQ
        11. Summary
      17. 10. Everything's a Class
        1. All Android UI elements are classes too
          1. Reintroducing references
            1. A quick break to throw out the trash
            2. Stack and heap – a quick summary
          2. So, how does this heap thing help me?
          3. Using Button and TextView widgets from our layout
          4. Inner and anonymous classes
        2. FAQ
        3. Summary
      18. 11. Widget Mania
        1. Exploring Android UI objects
          1. Declaring and initializing objects
          2. Widget news flash
        2. Exploring the palette
          1. EditText
          2. ImageView
          3. Radio button and group
            1. Anonymous classes
          4. A switch widget
          5. CheckBox
          6. WebView
          7. Date & Time
        3. Android permissions and Marshmallows
          1. Pre-Marshmallow permissions
          2. Marshmallow permissions
          3. The permission solution used in this book
        4. The Widget exploration mini app
          1. Setting up the widget exploration project and UI
          2. Coding the widget exploration app
        5. Summary
      19. 12. Having a Dialogue with the User
        1. Dialog windows
          1. The dialog demo mini project – introducing chaining
        2. About the Note To Self app
          1. Using naming conventions and String resources
          2. How to get the Note To Self code
          3. What we will do and when we will do it
          4. What the user can do
        3. Building the project and gathering resources
          1. Downloading the images
          2. Preparing the String resources
        4. Coding the Note class
        5. Implementing the dialog designs
        6. Coding the dialog boxes
          1. Coding the DialogNewNote class
          2. Coding the DialogShowNote class
          3. Showing our new dialogs
          4. Adding a + icon to the action bar
        7. Summary
      20. 13. Handling and Displaying Arrays of Data
        1. A random diversion
        2. Handling large amount of data with arrays
          1. Arrays are objects
        3. A simple array example mini app
        4. Getting dynamic with arrays
          1. A dynamic array example
        5. Entering the nth dimension with arrays
          1. A multidimensional array mini app
          2. Array out of bounds exceptions
        6. ArrayLists
          1. The enhanced for loop
        7. Arrays and ArrayLists are polymorphic
        8. ListView and BaseAdapter
          1. The problem with displaying lots of widgets
          2. The solution
          3. How to use ListView and BaseAdapter
          4. Inner classes revisited
        9. FAQ
        10. Summary
      21. 14. Handling and Displaying Notes in Note To Self
        1. Note To Self continued
          1. Updating onCreate and preparing the UI
          2. Creating a list item for ListView
        2. Improvements in Note To Self
          1. Persistence
          2. Animation
          3. Sound FX
          4. Problems with the design of the code
        3. FAQ
        4. Summary
      22. 15. Android Intent and Persistence
        1. Good Intents
          1. Switching Activity
          2. Passing data between activities
        2. Adding a settings page to the Note To Self app
          1. Creating SettingsActivity
          2. Designing the Settings screen's layout
          3. Enabling the user to switch to the Settings screen
        3. Persisting data with SharedPreferences
        4. Making the Note To Self app's settings persist
          1. Coding the SettingsActivity class
          2. Coding the MainActivity class
          3. Specifying a parent Activity
        5. More advanced persistence
          1. What is JSON?
          2. Java exceptions - try, catch, and finally
        6. Backing up user data in Note To Self
        7. FAQ
        8. Summary
      23. 16. UI Animations
        1. Animations in Android
          1. Designing cool animations in XML
            1. Fading in and out
            2. Movement
            3. Scaling or stretching
            4. Controlling the duration
            5. Rotating animations
            6. Repeating animations
            7. Combining animation properties with a set tag
          2. Instantiating animations and controlling them with Java code
          3. More animation features
            1. Listeners
            2. Animation interpolators
        2. The Animations Demo app – introducing SeekBar
          1. Laying out the animation demo
          2. Coding the XML animations
          3. Wiring up the Animations Demo app in Java
        3. Adding animations to Note To Self
          1. Defining the animations in XML
          2. Controlling the animations in Java
        4. FAQ
        5. Summary
      24. 17. Sound FX and Supporting Different Versions of Android
        1. Supporting multiple versions of Android
          1. Detecting the current Android version
          2. The SoundPool class
            1. Building SoundPool the new way and the old way
            2. Loading sound files into memory
            3. Playing a sound
            4. Stopping a sound
        2. Introducing the Spinner widget
          1. Making sound FX
          2. Laying out Sound Demo UI
          3. Coding the Sound Demo app
        3. Sound FX touches to Note To Self
        4. Deleting a note – introducing OnLongClick
        5. FAQ
        6. Summary
      25. 18. Design Patterns, Fragments, and the Real World
        1. Introducing the model-view-controller pattern
          1. The model
          2. The view
          3. The controller
        2. The imperfections of Note To Self revisited
        3. Android design guidelines
        4. Real-world apps
        5. The device detection mini app
        6. Configuration qualifiers
        7. Using configuration qualifiers – mini app
          1. The configuration qualifier's summary
        8. Fragments
          1. Fragments have a lifecycle too
            1. onCreate
            2. onCreateView
            3. onAttach and onDetach
            4. onStart, onPause, and onStop
          2. Managing Fragments with FragmentManager
        9. Our first working Fragment mini app
        10. Fragment reality check
        11. FAQ
        12. Summary
      26. 19. Using Multiple Fragments
        1. Using singletons for the model layer
        2. Inter-Fragment communications – interfaces revisited
        3. The dual-Fragment address book mini app
          1. Getting started
          2. Alias resources
          3. The NameAndAddress class
          4. Creating the communication interface
          5. Explaining the structure of the onListItemSelected method
          6. Explaining the code that starts a new Activity
          7. Explaining the code when the detail Fragment is already present
          8. What do we need to do?
          9. Creating the AddressDetailFragment class and layout
          10. Coding PortraitDetailActivity and the layout
        4. FAQ
        5. Summary
      27. 20. Paging and Swiping
        1. Building an image gallery/slider app
          1. Implementing the layout
          2. Coding the PagerAdapter class
          3. Coding the MainActivity class
          4. Running the gallery app
        2. Building a Fragment pager/slider app
          1. Coding the SimpleFragment class
          2. fragment_layout
          3. Coding the MainActivity class
          4. The activity_main layout
          5. Running the Fragment slider app
        3. Summary
      28. 21. Navigation Drawer and Where It's Snap
        1. Introducing the navigation drawer
        2. The Where it's snap app
          1. Preparing the resources
          2. Placeholder code for three Fragments
          3. Coding the MainActivity class and layout
            1. Switching between Fragments
            2. Setting up the drawer
            3. Handling the back button
        3. Summary
      29. 22. Capturing Images
        1. Capturing images using the camera
        2. The capturing images mini app
          1. Adding the camera permission to the manifest
          2. Defining a simple layout
          3. Coding the MainActivity class
          4. Running the Simple Photo app
        3. Where it's snap – coding the capture Fragment
          1. Creating the layout for the capture Fragment
          2. Coding the capture Fragment
          3. Testing the capture Fragment
        4. Summary
      30. 23. Using SQLite Databases in Our Apps
        1. Database 101
          1. What is a database?
          2. What is SQL?
          3. What is SQLite?
        2. The SQL syntax primer
          1. The SQLite example code
            1. Creating a table
            2. Inserting data into the database
            3. Retrieving data from the database
            4. Updating the database structure
        3. The Android SQLite API
          1. SQLiteOpenHelper and SQLiteDatabase
          2. Building and executing queries
          3. Database cursors
        4. The database mini app
          1. Implementing the layout
          2. Coding the database class
          3. Coding MainActivity
          4. Running the mini app
        5. Summary
      31. 24. Adding a Database to Where It's Snap
        1. The Photo class
        2. Handling the SQLite database
        3. Saving a new photo from the capture fragment
        4. Displaying a photo from the database
          1. Preparing the View layout
          2. Coding the ViewFragment class
        5. Coding the communications interface
        6. Coding TitlesFragment
        7. Coding TagsFragment
        8. Coding the fragment communications in MainActivity
        9. Running the app so far
        10. Summary
      32. 25. Integrating Google Maps and GPS Locations
        1. Global Positioning System
        2. Where in the world – the GPS mini app
          1. Where in the world – implementing a simple layout
          2. Coding the Where in the world mini app
          3. Running the Where in the world mini app
        3. The Google Maps mini app
          1. Preparing to connect to Google Maps
            1. Acquiring an API key
            2. Installing the Google Play services API
          2. Coding the maps_activity layout
          3. Coding MapsActivity
          4. Running the Simple Map app
        4. Summary
      33. 26. Upgrading SQLite – Adding Locations and Maps
        1. Adding locations and maps to Where it's Snap
          1. Updating the database
            1. Adding member variables to represent location data
            2. Updating the database version
            3. Adding code in onUpgrade to upgrade the database for existing users
            4. Updating the database creation code in onCreate for new users
            5. Updating the addPhoto method to handle GPS coordinates
          2. Updating the Photo class
          3. Updating CaptureFragment
        2. Updating ViewFragment
        3. Adding location permissions
        4. Testing the new map feature
        5. Summary
      34. 27. Going Local – Hola!
        1. The Localization mini app
          1. Preparing the layout
          2. Preparing the first strings.xml file
          3. Making the app multilingual with the second strings.xml file
          4. Adding a third language
          5. Running the app
        2. Localizing the Where it's snap app
        3. Summary
      35. 28. Threads, Touches, Drawing, and a Simple Game
        1. Threads
        2. Problems with threads
        3. Drawing with canvas and paint
          1. The Android coordinate system
        4. Android Canvas demo app
        5. Handling touches
        6. A simple game engine
        7. Building a simple game of Pong
          1. Adding sound files to the project
          2. Coding the ball
          3. Coding the paddle
          4. Coding MainActivity
          5. Coding SurfaceView
            1. Coding the update method
            2. Coding the draw method
          6. Lock the screen orientation and make it full-screen
          7. Running the game
          8. Pong enhancements
        8. Summary
      36. 29. Publishing Apps
        1. Preparing to publish
          1. Creating an app icon
          2. Preparing the required resources
        2. Building the publishable APK file
        3. Publishing the app
        4. Marketing
        5. More to see
        6. Summary
      37. 30. Before You Go
        1. What next
          1. Keep reading
            1. GitHub
            2. StackOverflow
            3. Android user forums
          2. For further study
          3. Make some apps!
        2. Goodbye and thank you
      38. Index