Android Programming with Kotlin for Beginners

Book description

Build smart looking Kotlin apps with UI and functionality for the Android platform

Key Features

  • Start your Android programming career, or just have fun publishing apps on Google Play marketplace
  • The first-principle introduction to Kotlin through Android, to start building easy-to-use apps
  • Learn by example and build four real-world apps and dozens of mini-apps

Book Description

Android is the most popular mobile operating system in the world and Kotlin has been declared by Google as a first-class programming language to build Android apps. With the imminent arrival of the most anticipated Android update, Android 10 (Q), this book gets you started building apps compatible with the latest version of Android.

It adopts a project-style approach, where we focus on teaching the fundamentals of Android app development and the essentials of Kotlin by building three real-world apps and more than a dozen mini-apps. The book begins by giving you a strong grasp of how Kotlin and Android work together before gradually moving onto exploring the various Android APIs for building stunning apps for Android with ease. You will learn to make your apps more presentable using different layouts. You will dive deep into Kotlin programming concepts such as variables, functions, data structures, Object-Oriented code, and how to connect your Kotlin code to the UI. You will learn to add multilingual text so that your app is accessible to millions of more potential users. You will learn how animation, graphics, and sound effects work and are implemented in your Android app.

By the end of the book, you will have sound knowledge about significant Kotlin programming concepts and start building your own fully featured Android apps.

What you will learn

  • Learn how Kotlin and Android work together
  • Build a graphical drawing app using Object-Oriented Programming (OOP) principles
  • Build beautiful, practical layouts using ScrollView, RecyclerView, NavigationView, ViewPager and CardView
  • Write Kotlin code to manage an apps' data using different strategies including JSON and the built-in Android SQLite database
  • Add user interaction, data captures, sound, and animation to your apps
  • Implement dialog boxes to capture input from the user
  • Build a simple database app that sorts and stores the user's data

Who this book is for

This book is for people who are new to Kotlin, Android and want to develop Android apps.It also acts as a refresher for those who have some experience in programming with Android and Kotlin.

Publisher resources

Download Example Code

Table of contents

  1. Android Programming with Kotlin for Beginners
    1. Table of Contents
    2. Android Programming with Kotlin for Beginners
      1. Why subscribe?
    3. Contributors
      1. About the author
      2. About the reviewers
      3. Packt is searching for authors like you
    4. Preface
      1. Who this book is for
      2. What this book covers
      3. To get the most out of this book
        1. Download the example code files
        2. Download the color images
        3. Conventions used
      4. Get in touch
        1. Reviews
    5. 1. Getting Started with Android and Kotlin
      1. Why use Kotlin and Android?
      2. The beginner's first stumbling block
      3. How Kotlin and Android work together
        1. The Android API
        2. Kotlin is object-oriented
        3. Run that by me again – what, exactly, is Android?
        4. Android Studio
      4. Setting up Android Studio
        1. Final step – for now
      5. What makes an Android app?
        1. Android resources
      6. The structure of Android's code
        1. Packages
        2. Classes
        3. Functions
      7. Our first Android app
        1. Possible extra step 1
        2. Possible extra step 2
      8. Deploying the app so far
        1. Running and debugging the app on an Android emulator
        2. Running the app on a real device
      9. Frequently asked question
      10. Summary
    6. 2. Kotlin, XML, and the UI Designer
      1. Examining the log output
        1. Filtering the logcat output
      2. Exploring the project's Kotlin code and the main layout's XML code
        1. Examining the MainActivity.kt file
          1. Code folding (hiding) in Android Studio
          2. The package declaration
          3. Importing classes
          4. The class declaration
          5. Functions inside the class
          6. A summary of the Kotlin code so far
        2. Examining the main layout file
          1. UI layout elements
          2. UI text elements
      3. Adding buttons to the main layout file
        1. Adding a button via the visual designer
          1. Editing the button's attributes
        2. Examining the XML code for the new button
        3. Adding a button by editing the XML code
        4. Giving the buttons unique id attributes
        5. Positioning the two buttons in the layout
        6. Making the buttons call different functions
      4. Leaving comments in our Kotlin code
      5. Coding messages to the user and the developer
      6. Writing our first Kotlin code
        1. Adding message code to the onCreate function
          1. Examining the output
        2. Writing our own Kotlin functions
          1. Examining the output
      7. Frequently asked questions
      8. Summary
    7. 3. Exploring Android Studio and the Project Structure
      1. A quick guided tour of Android Studio
      2. Project Explorer and project anatomy
        1. The Empty Activity project
        2. Exploring the Empty Activity project
          1. The manifests folder
          2. The java folder
          3. The res folder
          4. The res/drawable folder
          5. The res/layout folder
          6. The res/mipmap folder
          7. The res/values folder
            1. The colors.xml file
            2. The strings.xml file
            3. The styles.xml file
      3. The Basic Activity project
        1. Exploring the Basic Activity project
          1. The MainActivity.kt file
          2. The activity_main.xml file
          3. The extra functions in MainActivity.kt
          4. The content_main.xml file
      4. Exploring the Android emulator
        1. The emulator control panel
        2. Using the emulator as a real device
          1. Accessing the app drawer
          2. Viewing active apps and switching between apps
      5. Summary
    8. 4. Getting Started with Layouts and Material Design
      1. Material design
      2. Exploring Android UI design
      3. Layouts
      4. Creating the Exploring Layouts project
      5. Building a menu with LinearLayout
        1. Adding a LinearLayout to the project
        2. Preparing your workspace
        3. Examining the generated XML
        4. Adding a TextView to the UI
        5. Adding a multi-line TextView to the UI
      6. Wiring up the UI with the Kotlin code (part 1)
      7. Adding layouts within layouts
      8. Making the layout look pretty
      9. Wiring up the UI with the Kotlin code (part 2)
      10. Building a precise UI with ConstraintLayout
        1. Adding a CalenderView
        2. Resizing a view in a ConstraintLayout
        3. Using the Component Tree window
        4. Adding constraints manually
        5. Adding and constraining more UI elements
        6. Making the text clickable
      11. Laying out data with TableLayout
        1. Adding a TableRow to TableLayout
        2. Using the Component Tree when the visual designer won't do
        3. Organizing the table columns
        4. Linking back to the main menu
      12. Summary
    9. 5. Beautiful Layouts with CardView and ScrollView
      1. Attributes – a quick summary
        1. Sizing using dp
        2. Sizing fonts using sp
        3. Determining size with wrap or match
        4. Using padding and margin
        5. Using the layout_weight property
        6. Using gravity
      2. Building a UI with CardView and ScrollView
        1. Setting the view with Kotlin code
        2. Adding image resources
        3. Creating the content for the cards
        4. Defining dimensions for CardView
        5. Adding CardView to our layout
        6. Including layout files inside another layout
      3. Themes and material design
        1. Using the Android Studio theme designer
      4. Creating a tablet emulator
      5. Frequently asked question
      6. Summary
    10. 6. The Android Lifecycle
      1. The life and times of an Android app
        1. How Android interacts with our apps
      2. A simplified explanation of the Android lifecycle
        1. The lifecycle phases demystified
      3. How we handle the lifecycle phases
      4. The lifecycle demo app
        1. Coding the lifecycle demo app
        2. Running the lifecycle demo app
        3. Examining the lifecycle demo app output
      5. Some other overridden functions
      6. The structure of Kotlin code – revisited
      7. Summary
    11. 7. Kotlin Variables, Operators, and Expressions
      1. Learning the jargon
      2. More on code comments
      3. Variables
        1. Types of variables
        2. Declaring and initializing variables
        3. Saving keystrokes with type inference
      4. Operators and expressions
        1. The assignment operator
        2. The addition operator
        3. The subtraction operator
        4. The division operator
        5. The multiplication operator
        6. The increment operator
        7. The decrement operator
      5. The express yourself demo app
      6. Summary
    12. 8. Kotlin Decisions and Loops
      1. Making decisions in Kotlin
        1. Indenting code for clarity
        2. More Kotlin operators
          1. The comparison operator
          2. The logical NOT operator
          3. The NOT equal operator
          4. The greater-than operator
          5. The less-than operator
          6. The greater-than-or-equal-to operator
          7. The less-than-or-equal-to operator
          8. The logical AND operator
          9. The logical OR operator
        3. How to use all these operators to test variables
          1. Using the if expression
        4. If they come over the bridge, shoot them!
          1. Else do this instead
        5. Using when to make decisions
        6. The When Demo app
      2. Repeating code with loops
      3. while loops
      4. do-while loops
      5. Ranges
      6. For loops
      7. Controlling loops with break and continue
      8. Sample code
      9. Summary
    13. 9. Kotlin Functions
      1. Function basics and recap
        1. The basic function declaration
        2. Function parameter lists
        3. The return type and the return keyword
        4. Function bodies and single-expression functions
      2. Making functions flexible
        1. Default and named arguments
        2. Even more on functions
      3. Summary
    14. 10. Object-Oriented Programming
      1. Introducing OOP
        1. What is OOP exactly?
          1. Encapsulation
          2. Polymorphism
          3. Inheritance
        2. Why do it like this?
        3. Class recap
      2. Basic classes
        1. Declaring a class
        2. Instantiating a class
        3. Classes have functions and variables (kind of)
          1. Using the variables of a class
          2. Using the functions and variables of a class
          3. Class variables are properties
          4. Examples using properties with their getters, setters, and fields
          5. When to use overridden getters and setters
      3. Visibility modifiers
        1. Public
        2. Private
        3. Protected
        4. Internal
        5. Visibility modifiers summary
      4. Constructors
        1. Primary constructors
        2. Secondary constructors
          1. We need to talk about this
          2. Using the Meeting class
        3. Init blocks
      5. Basic classes app and using the init block
      6. Introduction to references
      7. Summary
    15. 11. Inheritance in Kotlin
      1. OOP and inheritance
      2. Using inheritance with open classes
        1. Basic inheritance examples
        2. Overriding functions
        3. Summary so far
      3. More polymorphism
        1. Abstract classes and functions
      4. Classes using the Inheritance example app
      5. Summary
    16. 12. Connecting Our Kotlin to the UI and Nullability
      1. All the Android UI elements are classes too
        1. A quick break to throw out the trash
        2. Seven useful facts about the Stack and the Heap
        3. So, how does this Heap thing help me?
      2. Kotlin interfaces
      3. Using buttons and TextView widgets from our layout with a little help from interfaces
      4. Nullability – val and var revisited
        1. Null objects
          1. Safe call operator
          2. Non null assertion
        2. Nullability in review
      5. Summary
    17. 13. Bringing Android Widgets to Life
      1. Declaring and initializing the objects from the layout
      2. Creating UI widgets from pure Kotlin without XML
      3. Exploring the palette – part 1
        1. The EditText widget
        2. The ImageView widget
        3. RadioButtons and RadioGroups
      4. Lambdas
        1. Writing the code for the overridden function
      5. Exploring the palette – part 2, and more lambdas
        1. The Switch widget
        2. The CheckBox widget
        3. The TextClock widget
      6. The widget exploration app
        1. Setting up the widget exploration project and UI
      7. Coding the widget exploration app
        1. Coding the CheckBox widget
          1. Changing transparency
          2. Changing color
          3. Changing size
        2. Coding the RadioButton widgets
          1. Using a lambda for handling clicks on a regular Button widget
          2. Coding the Switch widget
      8. Running the Widget Exploration app
      9. Converting layouts to ConstraintLayout
      10. Summary
    18. 14. Android Dialog Windows
      1. Dialog windows
        1. Creating the dialog demo project
        2. Coding a DialogFragment class
          1. Using chaining to configure the DialogFragment class
        3. Using the DialogFragment class
      2. The Note to self app
        1. Using String resources
        2. How to get the code files for the Note to self app
        3. The completed app
        4. Building the project
        5. Preparing the String resources
        6. Coding the Note class
        7. Implementing the dialog designs
        8. Coding the dialog boxes
          1. Coding the DialogNewNote class
          2. Coding the DialogShowNote class
        9. Showing and using our new dialogs
          1. Coding the floating action button
      3. Summary
    19. 15. Handling Data and Generating Random Numbers
      1. A random diversion
      2. Handling large amounts of data with arrays
        1. Arrays are objects
      3. A simple mini-app array example
      4. Getting dynamic with arrays
        1. A dynamic array example
      5. ArrayLists
      6. Arrays and ArrayLists are polymorphic
      7. Hashmaps
      8. The Note to self app
      9. Frequently asked questions
      10. Summary
    20. 16. Adapters and Recyclers
      1. Inner classes
      2. RecyclerView and RecyclerAdapter
        1. The problem with displaying lots of widgets
        2. The solution to the problem with displaying lots of widgets
        3. How to use RecyclerView and RecyclerAdapter
        4. What we will do to set up RecyclerView with RecyclerAdapter and an ArrayList of notes
      3. Adding RecyclerView, RecyclerAdapter, and ArrayList to the Note to Self project
        1. Removing the temporary "Show Note" button and adding RecyclerView
        2. Creating a list item for RecyclerView
        3. Coding the RecyclerAdapter class
          1. Coding the onCreateViewHolder function
          2. Coding the onBindViewHolder function
          3. Coding getItemCount
          4. Coding the ListItemHolder inner class
        4. Coding MainActivity to use the RecyclerView and RecyclerAdapter classes
          1. Adding code to onCreate
          2. Modifying the createNewNote function
          3. Coding the showNote function
      4. Running the app
      5. Frequently asked questions
      6. Summary
    21. 17. Data Persistence and Sharing
      1. The Android Intent class
        1. Switching Activity
        2. Passing data between Activities
      2. Adding a settings page to Note to self
        1. Creating the SettingsActivity
        2. Designing the settings screen layout
        3. Enabling the user to switch to the "Settings" screen
      3. Persisting data with SharedPreferences
      4. Reloading data with SharedPreferences
      5. Making the Note to self settings persist
        1. Coding the SettingsActivity class
        2. Coding the MainActivity class
      6. More advanced persistence
        1. What is JSON?
        2. Exceptions – try, catch, and finally
      7. Backing up user data in Note to self
      8. Frequently asked questions
      9. Summary
    22. 18. Localization
      1. Making the Note to self app Spanish, English, and German
        1. Adding Spanish support
        2. Adding German support
        3. Adding the String resources
      2. Running Note to self in German or Spanish
        1. Making the translations work in Kotlin code
      3. Summary
    23. 19. Animations and Interpolations
      1. Animations in Android
        1. Designing cool animations in XML
          1. Fading in and out
          2. Move it, move it
          3. Scaling or stretching
          4. Controlling the duration
          5. Rotate animations
          6. Repeating animations
          7. Combining an animation's properties with sets
        2. Instantiating animations and controlling them with Kotlin code
        3. More animation features
          1. Listeners
          2. Animation interpolators
      2. Animations demo app – introducing SeekBar
        1. Laying out the animation demo
        2. Coding the XML animations
        3. Wiring up the Animation demo app in Kotlin
      3. Frequently asked questions
      4. Summary
    24. 20. Drawing Graphics
      1. Understanding the Canvas class
        1. Getting started drawing with Bitmap, Canvas, and ImageView
          1. Canvas and Bitmap
          2. Paint
          3. ImageView and Activity
          4. Canvas, Bitmap, Paint, and ImageView – a quick summary
      2. Using the Canvas class
        1. Preparing the instances of the required classes
        2. Initializing the objects
        3. Setting the Activity content
      3. The Canvas Demo app
        1. Creating a new project
          1. Coding the Canvas demo app
            1. Exploring the Bitmap initialization
          2. Drawing on the screen
            1. Explaining Color.argb
      4. The Android coordinate system
        1. Plotting and drawing
      5. Creating bitmap graphics with the Bitmap class
      6. Manipulating bitmaps
        1. What is a bitmap?
        2. The Matrix class
          1. Inverting a bitmap to face the opposite direction
          2. Rotating the bitmap to face up and down
      7. The Bitmap manipulation demo app
        1. Adding the Bob graphic to the project
      8. Frequently asked question
      9. Summary
    25. 21. Threads and Starting the Live Drawing App
      1. Creating the Live Drawing project
      2. Looking ahead at the Live Drawing app
      3. Coding the MainActivity class
      4. Coding the LiveDrawingView class
        1. Adding the properties
        2. Coding the draw function
          1. Adding the printDebuggingText function
        3. Understanding the draw function and the SurfaceView class
      5. The game loop
      6. Threads
        1. Problems with threads
      7. Implementing the game loop with a thread
        1. Implementing Runnable and providing the run function
        2. Coding the thread
        3. Starting and stopping the thread
        4. Using the Activity lifecycle to start and stop the thread
        5. Coding the run function
      8. Running the app
      9. Summary
    26. 22. Particle Systems and Handling Screen Touches
      1. Adding custom buttons to the screen
      2. Implementing a particle system effect
        1. Coding the Particle class
        2. Coding the ParticleSystem class
        3. Spawning particle systems in the LiveDrawingView class
      3. Handling touches
        1. Coding the onTouchEvent function
        2. Finishing the HUD
      4. Running the app
      5. Summary
    27. 23. Android Sound Effects and the Spinner Widget
      1. The SoundPool class
        1. Initializing SoundPool
          1. Loading sound files into memory
          2. Playing a sound
          3. Stopping a sound
      2. Sound demo app introducing the Spinner widget
        1. Making sound effects
        2. Laying out the sound demo UI
        3. Coding the Sound demo
      3. Summary
    28. 24. Design Patterns, Multiple Layouts, and Fragments
      1. Introducing the model-view-controller pattern
        1. Model
        2. View
        3. Controller
      2. Android design guidelines
      3. Real-world apps
      4. Device detection mini app
        1. Coding the MainActivity class
          1. Unlocking the screen orientation
        2. Running the app
      5. Configuration qualifiers
        1. The limitation of configuration qualifiers
      6. Fragments
        1. Fragments have a life cycle too
          1. The onCreate function
          2. The onCreateView function
          3. The onAttach and onDetach functions
          4. The onStart, onPause, and onStop functions
        2. Managing fragments with FragmentManager
      7. Our first fragment app
      8. Fragment reality check
      9. Frequently asked question
      10. Summary
    29. 25. Advanced UI with Paging and Swiping
      1. The Angry Birds classic swipe menu
      2. 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
      3. Kotlin companion objects
      4. Building a Fragment Pager/slider app
        1. Coding the SimpleFragment class
        2. The fragment_layout
        3. Coding the MainActivity class
        4. The activity_main layout
        5. Running the fragment slider app
      5. Summary
    30. 26. Advanced UI with Navigation Drawer and Fragment
      1. Introducing the NavigationView
      2. Examining the Age Database app
        1. Insert
        2. Delete
        3. Search
        4. Results
      3. Starting the Age Database project
      4. Exploring the auto-generated code and assets
      5. Coding the Fragment classes and their layouts
        1. Creating the empty files for the classes and layouts
        2. Coding the classes
        3. Designing the layouts
        4. Designing content_insert.xml
        5. Designing content_delete.xml
        6. Designing content_search.xml
        7. Designing content_results.xml
      6. Using the Fragment classes and their layouts
        1. Editing the navigation drawer menu
        2. Adding a holder to the main layout
        3. Coding the MainActivity.kt file
      7. Summary
    31. 27. Android Databases
      1. Database 101
        1. What is a database?
        2. What is SQL?
        3. What is SQLite?
      2. The SQL syntax primer
        1. 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. Coding the database class
      5. Coding the Fragment classes to use the DataManager class
      6. Running the Age Database app
      7. Summary
    32. 28. A Quick Chat Before You Go
      1. Publishing
      2. Making an app!
      3. Carrying on learning
        1. Carrying on reading
        2. GitHub
        3. StackOverflow
        4. Android user forums
        5. Higher-level study
      4. My other channels
      5. Goodbye and thank you
    33. A. Other Book You May Enjoy
      1. Leave a review - let other readers know what you think
    34. Index

Product information

  • Title: Android Programming with Kotlin for Beginners
  • Author(s): John Horton
  • Release date: April 2019
  • Publisher(s): Packt Publishing
  • ISBN: 9781789615401