Skip to main content
main-content
Top

About this book

Build Android apps and learn the essentials of the popular Kotlin programming language and APIs. This book will teach you the key Kotlin skills and techniques important for creating your very own Android apps. Apart from introducing Kotlin programming, Learn Kotlin for Android Development stresses clean code principles and introduces object-oriented and functional programming as a starting point for developing Android apps.
After reading and using this book, you'll have a foundation to take away and apply to your own Kotlin-based Android app development. You'll be able to write useful and efficient Kotlin-based apps for Android, using most of the features Kotlin as a language has to offer.
What You Will LearnBuild your first Kotlin app that runs on Android
Work with Kotlin classes and objects for Android
Use constructs, loops, decisions, and scopes
Carry out operations on data
Master data containers, arrays, and collections
Handle exceptions and access external libraries
Who This Book Is For
Very little programming experience is required: no prior knowledge of Kotlin needed.

Table of Contents

Chapter 1. Your First Kotlin Application: Hello Kotlin

Abstract
In this chapter we are going to learn how to use the Android Studio integrated development environment (IDE) to write and execute a first simple Kotlin program.
Peter Späth

Chapter 2. Classes and Objects: Object Orientation Philosophy

Abstract
At the beginning of the book we said that computer programs are about processing some input and generating some output from it, possibly also altering the state of some data-holding instance like a file or a database. Although this is certainly true, it does not tell the whole story. In real-world scenarios, computer programs exhibit another characteristic: They are supposed to be of some practical use, and as a result of that, model real-world events and objects.
Peter Späth

Chapter 3. Classes at Work: Properties and Functions

Abstract
After reading Chapter 2 about classes and objects, it is now time shine more light on properties and their types, and also on the options we have to declare functions and what can be done from inside functions. This chapter talks about property and function declarations, but also about an important feature of object-oriented languages, inheritance, through which properties and functions of some class can be altered and redefined by other classes. We also learn about visibility and encapsulation, which help us to improve program structure.
Peter Späth

Chapter 4. Classes and Objects: Extended Features

Abstract
This chapter covers some extended object orientation features that are not necessary for a program to work, but nevertheless improve readability and expressiveness. The chapter presumes that you have read Chapter 2. We also use the NumberGuess sample app from Chapter 2.
Peter Späth

Chapter 5. ExpressionsExpressions: Operations on Data

Abstract
We already used expressions a couple of times. Whenever you need to assign a value to a variable, need function call parameters, or need to provide a value to some kind of language construct, you need an expression. Expressions also show up where you don’t expect them, and they can be ignored if we don’t need them.
Peter Späth

Chapter 6. Comments in Kotlin Files

Abstract
Comments in computer language files are text that does not belong to the computer language itself and thus have no influence over the program execution, but provide a textual description of elements and constructs used in the program. Comments help the reader to understand your program.
Peter Späth

Chapter 7. Structural ConstructsStructural constructs

Abstract
From the very beginning of computer languages, conditional branching of the program flow has been one of the most basic things program code must be able to express. This branching happens inside functions, so it imposes some kind of substructuring inside classes and singleton objects. In this chapter we cover such branching constructs, together with auxiliary classes that help us write corresponding code.
Peter Späth

Chapter 8. Exceptions: If Something Goes Wrong

Abstract
For very simple programs it is probably easy to make sure all program parts do exactly what they are supposed to. For programs with a higher level of complexity, those built by many developers, or those that use external programs (libraries), the situation is not that clear. Problems will arise, for example, if lists or arrays get addressed out of bounds, some I/O access to files or network data streams fails, or objects end up in an unanticipated or corrupted state.
Peter Späth

Chapter 9. Data Containers

Abstract
Nature and human civilization are about collections. Families collect relatives, cities collect people acting together, houses collect people and their belongings, math set theory uses collections for relation formulas, galaxies collect stars, atoms collect elementary particles, and so on. It is thus no surprise that computer languages, intended to model real-world scenarios, must be able to model collections as well.
Peter Späth

Chapter 10. True, False, and Undecided: Nullability

Abstract
At school you learned about the dichotomy of true versus false, and you probably heard that there is nothing else. Reading this book, thus far, you have learned that in Kotlin there exists a boolean type Boolean with exactly those possible values: true and false. Period. Really?
Peter Späth

Chapter 11. Handling Equality

Abstract
There is a strong distinction between identity and equality. Two things are identical if they actually are the same. If you bought a white candle this morning, let’s call it A, the white candle in your shopping bag and the white candle that this afternoon is placed in your candleholder are the same and thus identical (presume this is the only candle you own). Now assume you bought a second candle, B, of the same type from the same manufacturer. Apart from some linguistic fluffiness you sometimes hear, those two candles are not the same. Candles A and B are not the same, but they are equal. This is because they have the same characteristics: the same color, the same weight, the same diameter, and the same length. Hold on, though: This is not necessarily true. The manufacturer says such a candle weights 300 g, but a high-precision balance tells us candle A weighs 300.00245 g, and candle B weighs 299.99734 g. If you take a kitchen scale, though, the weights of candles A and B are the same. Therefore you can see that equality depends on strictness and it is relative.
Peter Späth

Chapter 12. Back to Math: Functional Programming

Abstract
If you look at the examples and exercises presented so far in this book you can see that we fluctuate between two styles of programming: [statement1] // do something [statement2] // do something [statement3] // do something …
Peter Späth

Chapter 13. About Type Safety: Generics

Abstract
Generics is a term used for denoting a set of language features that allow us to add type parameters to types. Consider, for example, a simple class with a function for adding elements in the form of Int objects: class AdderInt {     fun add(i:Int) {         …     } }
Peter Späth

Chapter 14. Adding Hints: Annotations

Abstract
Annotations are for adding meta-information to your code. What does that mean? Consider the following classes: class Adder {     fun add(a:Double, b:Double) = a + b } class Subtractor {     fun subtract(a:Double, b:Double) = a - b }
Peter Späth

Chapter 15. Using the Java and Kotlin APIs

Abstract
Kotlin has a language kernel that handles classes, objects, properties, functions, structural constructs, and all that stuff. We’ve been talking about these a lot to this point. Once in a while we have mentioned and used the term Kotlin standard library without explicitly stating what that actually is. In real life a library is a place where much information is available. Whenever you need to know something you can go there and try to find a book that can show you what things are or how they work, or what you have to do to achieve something. For a computer language, a library is something similar: a place with lots of classes and functions that you can use for certain tasks. We already talked about collections, which are governed by library classes and functions.
Peter Späth

Chapter 16. The Collections API

Abstract
We already talked about collections in Chapter 9, namely lists, sets, and maps. The collections API, however, is extensive and contains more classes and interfaces than we described in Chapter 9. For Java the API even gets called the collections framework. Without claiming to be exhaustive in this chapter, we revise what we already know and also talk about a couple of more interesting collection interfaces, classes, and functions.
Peter Späth

Chapter 17. More APIs

Abstract
This chapter gathers a couple of more APIs you can use in your app. First we have the math API that gets used for mathematical calculations. For date and time handling, including transformations between different time representations, and parsing and formatting dates and times, we describe the date and time API. For input and output, which for Android boils down to file handling, we give an overview of the input and output API. For dynamically acquiring class member information the reflection API gets used; this is not a prominent part of object orientation but can help under some circumstances, so we include a treatise on reflection. A very powerful means to investigate and manipulate patterns inside strings is evident if we look at regular expressions, so we finish the chapter with a survey of regular expression constructs.
Peter Späth

Chapter 18. Working in Parallel: Multithreading

Abstract
Modern computers and modern smartphones have several CPUs able to do work in parallel. You probably think of several apps running at the same time, but there is more to concurrency; you can have several “actors” do work in parallel in one app, noticeably speeding up program execution. I deliberately say “actors” because simply saying that several CPUs do work in parallel only covers part of the story. In fact, software developers prefer to think of threads, which are program sequences that can potentially run independent of each other. Which CPU actually runs a thread is left to the process scheduling managed by the operating system. We adopt that thread notion and by that abstract from operation system process handling and hardware execution internals.
Peter Späth

Chapter 19. Using External Libraries

Abstract
External libraries are collections of interfaces and classes thst are of general use and thus can be reused in various projects. You won’t find many Kotlin libraries yet, but because it is easy for Kotlin to interfere with Java classes and interfaces, in your projects you can use one or some of the plethora of Java libraries published by other developers and development teams.
Peter Späth

Chapter 20. XML and JSON

Abstract
In Chapter 19 we learned how to include external libraries in our Android projects. Kotlin doesn’t have dedicated XML and JSON processing classes included in its standard library, so to achieve XML- and JSON-related tasks we use appropriate external libraries and add some convenience functions in the form of extension functions.
Peter Späth
Additional information