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