ebook img

How to Build Android Apps with Kotlin: A hands-on guide to developing, testing and publishing your first apps with Android PDF

795 Pages·2021·21.142 MB·English
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview How to Build Android Apps with Kotlin: A hands-on guide to developing, testing and publishing your first apps with Android

How to Build Android Apps with Kotlin A hands-on guide to developing, testing, and publishing your first apps with Android Alex Forrester, Eran Boudjnah, Alexandru Dumbravan, and Jomar Tigcal How to Build Android Apps with Kotlin Copyright © 2021 Packt Publishing All rights reserved. No part of this course may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews. Every effort has been made in the preparation of this course to ensure the accuracy of the information presented. However, the information contained in this course is sold without warranty, either express or implied. Neither the authors, nor Packt Publishing, and its dealers and distributors will be held liable for any damages caused or alleged to be caused directly or indirectly by this course. Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this course by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information. Authors: Alex Forrester, Eran Boudjnah, Alexandru Dumbravan, and Jomar Tigcal Reviewers: Niharika Arora, Gaurav Chandorkar, and Kaushal Dhruw Managing Editor: Anushree Arun Tendulkar Acquisitions Editors: Royluis Rodrigues and Anindya Sil Production Editor: Salma Patel Editorial Board: Megan Carlisle, Mahesh Dhyani, Heather Gopsill, Manasa Kumar, Alex Mazonowicz, Monesh Mirpuri, Bridget Neale, Abhishek Rane, Brendan Rodrigues, Ankita Thakur, Nitesh Thakur, and Jonathan Wray First published: February 2021 Production reference: 1250221 ISBN: 978-1-83898-411-3 Published by Packt Publishing Ltd. Livery Place, 35 Livery Street Birmingham B3 2PB, UK Table of Contents Preface i Chapter 1: Creating Your First App 1 Introduction .............................................................................................. 2 Creating an Android Project with Android Studio ............................... 2 Exercise 1.01: Creating an Android Studio Project for Your App ........... 4 Setting Up a Virtual Device and Running Your App ............................. 8 Exercise 1.02: Setting Up a Virtual Device and Running Your App on It ............................................................................................. 10 The Android Manifest ........................................................................... 18 Exercise 1.03: Configuring the Android Manifest Internet Permission ................................................................................... 21 Using Gradle to Build, Configure, and Manage App Dependencies ........................................................................................ 26 Project-Level build.gradle file ................................................................... 26 App-Level build.gradle ............................................................................... 28 Exercise 1.04: Exploring how Material Design is used to theme an app ........................................................................................ 33 Android Application Structure ............................................................ 34 Exercise 1.05: Adding Interactive UI Elements to Display a Bespoke Greeting to the User ............................................. 43 Accessing Views in layout files ................................................................. 51 Further Input Validation ........................................................................... 51 Activity 1.01: Producing an App to Create RGB Colors .......................... 52 Summary ................................................................................................ 54 Chapter 2: Building User Screen Flows 57 Introduction ........................................................................................... 58 The Activity Lifecycle ............................................................................ 58 Exercise 2.01: Logging the Activity Callbacks ......................................... 60 Saving and Restoring the Activity State ............................................. 69 Exercise 2.02: Saving and Restoring the State in Layouts ..................... 69 Exercise 2.03: Saving and Restoring the State with Callbacks .............. 79 Activity Interaction with Intents ......................................................... 84 Exercise 2.04: An Introduction to Intents ............................................... 85 Exercise 2.05: Retrieving a Result from an Activity ............................... 96 Intents, Tasks, and Launch Modes ................................................... 110 Exercise 2.06: Setting the Launch Mode of an Activity ........................ 112 Activity 2.01: Creating a Login Form ...................................................... 119 Summary .............................................................................................. 121 Chapter 3: Developing the UI with Fragments 123 Introduction ......................................................................................... 124 The Fragment Lifecycle ...................................................................... 124 onAttach .................................................................................................... 125 onCreate ................................................................................................... 125 onCreateView ........................................................................................... 126 onViewCreated ......................................................................................... 126 onActivityCreated .................................................................................... 126 onStart ....................................................................................................... 126 onResume ................................................................................................. 126 onPause ..................................................................................................... 127 onStop ....................................................................................................... 127 onDestroyView ......................................................................................... 127 onDestroy ................................................................................................. 127 onDetach ................................................................................................... 127 Exercise 3.01: Adding a Basic Fragment and the Fragment Lifecycle ............................................................................ 128 Exercise 3.02: Adding Fragments Statically to an Activity .................. 138 Static Fragments and Dual-Pane Layouts ........................................ 149 Exercise 3.03: Dual-Pane Layouts with Static Fragments ................... 149 Dynamic Fragments ............................................................................ 169 Exercise 3.04: Adding Fragments Dynamically to an Activity ............. 169 Jetpack Navigation .............................................................................. 174 Exercise 3.05: Adding a Jetpack Navigation Graph .............................. 174 Activity 3.01: Creating a Quiz on the Planets ........................................ 181 Summary .............................................................................................. 185 Chapter 4: Building App Navigation 187 Introduction ......................................................................................... 188 Navigation Overview .......................................................................... 188 Navigation Drawer .............................................................................. 189 Exercise 4.01: Creating an App with a Navigation Drawer ................. 191 Bottom Navigation ............................................................................. 213 Exercise 4.02: Adding Bottom Navigation to Your App ....................... 213 Tabbed Navigation .............................................................................. 226 Exercise 4.03: Using Tabs for App Navigation ...................................... 226 Activity 4.01: Building Primary and Secondary App Navigation ......... 238 Summary .............................................................................................. 240 Chapter 5: Essential Libraries: Retrofit, Moshi, and Glide 243 Introduction ......................................................................................... 244 Fetching Data from a Network Endpoint ......................................... 246 Exercise 5.01: Reading Data from an API .............................................. 249 Parsing a JSON Response ................................................................... 255 Exercise 5.02: Extracting the Image URL from the API Response ...... 257 Loading Images from a Remote URL ................................................ 260 Exercise 5.03: Loading the Image from the Obtained URL ................. 264 Activity 5.01: Displaying the Current Weather ..................................... 271 Summary .............................................................................................. 273 Chapter 6: RecyclerView 275 Introduction ......................................................................................... 276 Adding RecyclerView to Our Layout ................................................. 277 Exercise 6.01: Adding an Empty RecyclerView to Your Main Activity ............................................................................... 277 Populating the RecyclerView ............................................................. 280 Exercise 6.02: Populating Your RecyclerView ....................................... 288 Responding to Clicks in RecyclerView .............................................. 298 Exercise 6.03: Responding to Clicks ....................................................... 299 Supporting Different Item Types ...................................................... 302 Exercise 6.04: Adding Titles to RecyclerView ........................................ 306 Swiping to Remove Items .................................................................. 312 Exercise 6.05: Adding Swipe to Delete Functionality ........................... 315 Adding Items Interactively ................................................................. 319 Exercise 6.06: Implementing an "Add A Cat" Button ........................... 322 Activity 6.01: Managing a List of Items .................................................. 325 Summary .............................................................................................. 327 Chapter 7: Android Permissions and Google Maps 329 Introduction ......................................................................................... 330 Requesting Permissions from the User ........................................... 330 Exercise 7.01: Requesting the Location Permission ............................ 336 Showing a Map of the User's Location ............................................. 344 Exercise 7.02: Obtaining the User's Current Location ......................... 347 Map Clicks and Custom Markers ...................................................... 351 Exercise 7.03: Adding a Custom Marker Where the Map Was Clicked ............................................................................... 355 Activity 7.01: Creating an App to Find the Location of a Parked Car ......................................................................................... 361 Summary .............................................................................................. 362 Chapter 8: Services, WorkManager, and Notifications 365 Introduction ......................................................................................... 366 Starting a Background Task Using WorkManager .......................... 367 Exercise 8.01: Executing Background Work with the WorkManager Class .......................................................................... 370 Background Operations Noticeable to the User – Using a Foreground Service ............................................................... 376 Exercise 8.02: Tracking Your SCA's Work with a Foreground Service ............................................................................... 382 Activity 8.01: Reminder to Drink Water ................................................. 390 Summary .............................................................................................. 391 Chapter 9: Unit Tests and Integration Tests with JUnit, Mockito, and Espresso 393 Introduction ......................................................................................... 394 JUnit ...................................................................................................... 396 Android Studio Testing Tips ............................................................... 405 Mockito ................................................................................................ 407 mockito-kotlin .......................................................................................... 412 Exercise 9.01: Testing the Sum of Numbers ......................................... 414 Integration Tests ................................................................................. 417 Robolectric ................................................................................................ 418 Espresso .................................................................................................... 422 Exercise 9.02: Double Integration .......................................................... 424 UI Tests ................................................................................................ 433 Exercise 9.03: Random Waiting Times ................................................... 440 Test-Driven Development .................................................................. 446 Exercise 9.04: Using TDD to Calculate the Sum of Numbers .............. 448 Activity 9.01: Developing with TDD ........................................................ 451 Summary .............................................................................................. 453 Chapter 10: Android Architecture Components 457 Introduction ......................................................................................... 458 ViewModel and LiveData ................................................................... 460 ViewModel ................................................................................................ 460 LiveData .................................................................................................... 463 Exercise 10.01: Creating a Layout with Configuration Changes ......... 465 Exercise 10.02: Adding a ViewModel ..................................................... 472 Exercise 10.03: Sharing our ViewModel between the Fragments ...... 475 Exercise 10.04: Adding LiveData ............................................................ 477 Room .................................................................................................... 484 Entities ....................................................................................................... 486 DAO ............................................................................................................ 488 Setting Up the Database ......................................................................... 491 Third-Party Frameworks ......................................................................... 495 Exercise 10.05: Making a Little Room .................................................... 497 Customizing Life Cycles ...................................................................... 502 Exercise 10.06: Reinventing the Wheel .................................................. 503 Activity 10.01: Shopping Notes App ....................................................... 506 Summary .............................................................................................. 509 Chapter 11: Persisting Data 511 Introduction ......................................................................................... 512 Repository ............................................................................................ 513 Exercise 11.01: Creating a Repository ................................................... 514 Exercise 11.02: Adding Error Handling .................................................. 524 Preferences .......................................................................................... 529 SharedPreferences .................................................................................. 530 Exercise 11.03: Wrapping SharedPreferences ...................................... 531 PreferenceFragment ............................................................................... 536 Exercise 11.04: Customized Settings ..................................................... 538 Files ....................................................................................................... 545 Internal Storage ....................................................................................... 546 External Storage ....................................................................................... 548 FileProvider ............................................................................................... 549 Storage Access Framework (SAF) ........................................................... 550

See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.