Android Programming with Kotlin for Beginners

Android Programming with Kotlin for Beginners

Android Programming with Kotlin for Beginners Build Android apps starting from zero programming experience with the new Kotlin programming language John Horton BIRMINGHAM - MUMBAI Android Programming with Kotlin for Beginners Copyright © 2019 Packt Publishing All rights reserved. No part of this book 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 book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author, nor Packt Publishing or its dealers and distributors, will be held liable for any damages caused or alleged to have been caused directly or indirectly by this book. Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information. Commissioning Editor: Pavan Ramchandani Acquisition Editor: Larissa Pinto Content Development Editor: Smit Carvalho Technical Editor: Surabhi Kulkarni Copy Editor: Safis Editing Project Coordinator: Kinjal Bari Proofreader: Safis Editing Indexers: Pratik Shirodkar Graphics: Alishon Mendonsa Production Coordinator: Arvindkumar Gupta First published: April 2019 Production reference: 1260419 Published by Packt Publishing Ltd. Livery Place 35 Livery Street Birmingham B3 2PB, UK. ISBN 978-1-78961-540-1 www.packtpub.com I think that everybody has an app inside of them, and all you need do is work hard enough to get it out of you. For Jo, Jack, James, Ray, Rita, and Missy. mapt.io Mapt is an online digital library that gives you full access to over 5,000 books and videos, as well as industry leading tools to help you plan your personal development and advance your career. For more information, please visit our website. Why subscribe? • Spend less time learning and more time coding with practical eBooks and Videos from over 4,000 industry professionals • Learn better with Skill Plans built especially for you • Get a free eBook or video every month • Mapt is fully searchable • Copy and paste, print, and bookmark content Packt.com Did you know that Packt offers eBook versions of every book published, with PDF and ePub files available? You can upgrade to the eBook version at www.Packt.com and as a print book customer, you are entitled to a discount on the eBook copy. Get in touch with us at [email protected] for more details. At www.Packt.com, you can also read a collection of free technical articles, sign up for a range of free newsletters, and receive exclusive discounts and offers on Packt books and eBooks. Contributors About the author John Horton is a programming and gaming enthusiast based in the UK. He has a passion for writing apps, games, books, and blog articles. He is the founder of Game Code School. About the reviewers Ashok Kumar S has been working in the mobile development domain for about six years. In his early days, he was a JavaScript and Node developer. With his strong web development skills, he mastered web and mobile development. He is a Google-certified engineer, a speaker at global conferences, such as DroidCon Berlin and MODS, and he also runs a YouTube channel called AndroidABCD for Android developers. He also contributes heavily to open source to improve his e-karma. He has written books on WearOS programming and the Firebase toolchain. Ashok has also reviewed books on mobile and web development, namely Mastering Junit5, Android Programming for Beginners, and Developing Enterprise Applications Using JavaScript. Mitchell Wong Ho was born in Johannesburg, South Africa, where he completed his national diploma in electrical engineering. Mitchell's software development career started on embedded systems and then moved to Microsoft desktop/server applications. Mitchell has been programming in Java since 2000 on J2ME, JEE, desktop, and Android applications, and has more recently been advocating Kotlin for Android. Packt is searching for authors like you If you're interested in becoming an author for Packt, please visit authors.packtpub. com and apply today. We have worked with thousands of developers and tech professionals, just like you, to help them share their insight with the global tech community. You can make a general application, apply for a specific hot topic that we are recruiting an author for, or submit your own idea. Table of Contents Preface xv Chapter 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 3 The Android API 5 Kotlin is object-oriented 6 Run that by me again – what, exactly, is Android? 7 Android Studio 8 Setting up Android Studio 9 Final step – for now 16 What makes an Android app? 17 Android resources 17 The structure of Android's code 18 Packages 18 Classes 19 Functions 19 Our first Android app 19 Possible extra step 1 24 Possible extra step 2 25 Deploying the app so far 25 Running and debugging the app on an Android emulator 26 Running the app on a real device 29 Frequently asked question 30 Summary 31 [ i ] Table of Contents Chapter 2: Kotlin, XML, and the UI Designer 33 Examining the log output 33 Filtering the logcat output 35 Exploring the project's Kotlin code and the main layout's XML code 35 Examining the MainActivity.kt file 35 Code folding (hiding) in Android Studio 36 The package declaration 37 Importing classes 37 The class declaration 38 Functions inside the class 38 A summary of the Kotlin code so far 39 Examining the main layout file 39 UI layout elements 41 UI text elements 42 Adding buttons to the main layout file 43 Adding a button via the visual designer 43 Editing the button's attributes 45 Examining the XML code for the new button 48 Adding a button by editing the XML code 49 Giving the buttons unique id attributes 51 Positioning the two buttons in the layout 52 Making the buttons call different functions 55 Leaving comments in our Kotlin code 56 Coding messages to the user and the developer 57 Writing our first Kotlin code 57 Adding message code to the onCreate function 58 Examining the output 59 Writing our own Kotlin functions 60 Examining the output 62 Frequently asked questions 63 Summary 64 Chapter 3: Exploring Android Studio and the Project Structure 65 A quick guided tour of Android Studio 66 Project Explorer and project anatomy 68 The Empty Activity project 68 Exploring the Empty Activity project 70 The manifests folder 71 The java folder 74 The res folder 76 The res/drawable folder 76 The res/layout folder 77 The res/mipmap folder 78 The res/values folder 79 [ ii ] Table of Contents The Basic Activity project 84 Exploring the Basic Activity project 85 The MainActivity.kt file 85 The activity_main.xml file 86 The extra functions in MainActivity.kt 88 The content_main.xml file 89 Exploring the Android emulator 89 The emulator control panel 90 Using the emulator as a real device 92 Accessing the app drawer 93 Viewing active apps and switching between apps 94 Summary 95 Chapter 4: Getting Started with Layouts and Material Design 97 Material design 98 Exploring Android UI design 98 Layouts 99 Creating the Exploring Layouts project 99 Building a menu with LinearLayout 100 Adding a LinearLayout to the project 101 Preparing your workspace 102 Examining the generated XML 103 Adding a TextView to the UI 103 Adding a multi-line TextView to the UI 106 Wiring up the UI with the Kotlin code (part 1) 108 Adding layouts within layouts 110 Making the layout look pretty 114 Wiring up the UI with the Kotlin code (part 2) 116 Building a precise UI with ConstraintLayout 116 Adding a CalenderView 116 Resizing a view in a ConstraintLayout 117 Using the Component Tree window 118 Adding constraints manually 120 Adding and constraining more UI elements 121 Making the text clickable 125 Laying out data with TableLayout 126 Adding a TableRow to TableLayout 126 Using the Component Tree when the visual designer won't do 127 Organizing the table columns 128 Linking back to the main menu 130 Summary 131 [ iii ] Table of Contents Chapter 5: Beautiful Layouts with CardView and ScrollView 133 Attributes – a quick summary 133 Sizing using dp 134 Sizing fonts using sp 134 Determining size with wrap or match 135 Using padding and margin 137 Using the layout_weight property 138 Using gravity 139 Building a UI with CardView and ScrollView 141 Setting the view with Kotlin code 142 Adding image resources 142 Creating the content for the cards 143 Defining dimensions for CardView 147 Adding CardView to our layout 147 Including layout files inside another layout 149 Themes and material design 152 Using the Android Studio theme designer 153 Creating a tablet emulator 156 Frequently asked question 159 Summary 159 Chapter 6: The Android Lifecycle 161 The life and times of an Android app 162 How Android interacts with our apps 162 A simplified explanation of the Android lifecycle 163 The lifecycle phases demystified 164 How we handle the lifecycle phases 165 The lifecycle demo app 168 Coding the lifecycle demo app 168 Running the lifecycle demo app 171 Examining the lifecycle demo app output 172 Some other overridden functions 173 The structure of Kotlin code – revisited 175 Summary 176 Chapter 7: Kotlin Variables, Operators, and Expressions 177

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    699 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us