Book description
Discover Android programming and web development by understanding the concepts of Kotlin Programming
About This Book
- Practical solutions to your common programming problems with Kotlin 1.1
- Leverage the functional power of Kotlin to ease your Android application development
- Learn to use Java code in conjunction with Kotlin
Who This Book Is For
This book will appeal to Kotlin developers keen to find solutions for their common programming problems. Java programming knowledge would be an added advantage.
What You Will Learn
- Understand the basics and object-oriented concepts of Kotlin Programming
- Explore the full potential of collection frameworks in Kotlin
- Work with SQLite databases in Android, make network calls, and fetch data over a network
- Use Kotlin's Anko library for efficient and quick Android development
- Uncover some of the best features of Kotlin: Lambdas and Delegates
- Set up web service development environments, write servlets, and build RESTful services with Kotlin
- Learn how to write unit tests, integration tests, and instrumentation/acceptance tests.
In Detail
The Android team has announced first-class support for Kotlin 1.1. This acts as an added boost to the language and more and more developers are now looking at Kotlin for their application development. This recipe-based book will be your guide to learning the Kotlin programming language.
The recipes in this book build from simple language concepts to more complex applications of the language. After the fundamentals of the language, you will learn how to apply the object-oriented programming features of Kotlin 1.1. Programming with Lambdas will show you how to use the functional power of Kotlin.
This book has recipes that will get you started with Android programming with Kotlin 1.1, providing quick solutions to common problems encountered during Android app development. You will also be taken through recipes that will teach you microservice and concurrent programming with Kotlin. Going forward, you will learn to test and secure your applications with Kotlin. Finally, this book supplies recipes that will help you migrate your Java code to Kotlin and will help ensure that it's interoperable with Java.
Style and approach
This book explains concepts related to Kotlin Programming using a practical approach and with the help of easy-to-follow recipes.
Table of contents
- Preface
-
Installation and Working with Environment
- Introduction
- Creating Kotlin Android project
- How to use Gradle to run Kotlin code
- How to run a Kotlin compiled class
- How to build a self-executable JAR with Gradle and Kotlin
- Reading console input in Kotlin
- Converting Java code to Kotlin and vice versa
- How to write an idiomatic logger in Kotlin
- Escaping for Java identifiers that are keywords in Kotlin
- Disambiguating using the "as" keyword to locally rename the clashing entity
- Doing bit manipulations in Kotlin
- Parsing String to Long, Double, or Int
- Using String templates in Kotlin
-
Control Flow
- Introduction
- Assigning result to an expression using the if keyword
- Using range with the when expression
- Using when with custom objects
- Using try–catch as an expression
- How to write a swap function in Kotlin using the also function
- How to throw a custom exception in Kotlin
- How to make a multiconditional loop in Kotlin
-
Classes and Objects
- Introduction
- Initializing body of constructor
- Converting one data type into another
- How to type check an object
- How to work with an abstract class in Kotlin
- How to iterate over a class's properties in Kotlin
- How to work with inline properties
- How to work with nested class
- Getting class in Kotlin
- Working with delegated properties
- Working with enums
-
Functions
- Introduction
- Specifying default values in functions
- Using named arguments in functions
- Creating the RecyclerView Adapter in Kotlin
- Creating getter setters in Kotlin
- Passing variable arguments to a function
- Passing a function as a parameter to another
- Declaring a static function
- Using the use keyword in Kotlin
- Working with closures
- Function literals with receivers
- Working with anonymous functions
-
Object-Oriented Programming
- Introduction
- Working with interfaces in Kotlin
- How to implement complicated interfaces with multiple overridden methods in Kotlin
- How to extend a class in Kotlin (Inheritance and Extension functions)
- How to work with Generics in Kotlin
- How to implement polymorphism in Kotlin
- Restricting class hierarchies
-
Collections Framework
- Introduction
- How to merge two collections
- Splitting original collection into pair of collections
- Sorting a list by specified comparator
- Sorting in descending order
- Parsing a JSON response using Gson
- How to filter and map using lambda expressions
- How to sort a list of objects and keep null objects at the end
- How to implement a lazy list in Kotlin
- How to pad a string in Kotlin
- How to flatten an array or map
- How to sort collection by multiple fields in Kotlin
- How to use limit in Kotlin list
- How to create a 2D array in Kotlin
- How to skip the first "n" entries in Kotlin
-
Handling File Operations in Kotlin
- Introduction
- Reading from files using InputReader
- Reading all lines in a file using InputReader
- Reading line by line using InputReader
- Reading from files using BufferedReader
- Reading all lines in a file using BufferedReader
- Reading line by line using bufferedReader
- Reading string and JSON over network
-
Anko Commons and Extension Function
- Introduction
- Setting up Anko with Gradle
- Extending Android framework using extension function
- Using extensions as properties
- Using intents with Anko
- Making a call intent using Anko
- Sending a text intent using Anko
- Browsing the web browser using Anko
- Sharing some text using intents in Anko
- Sending an email using Anko
- Creating Android dialogs with Anko
- Showing an alert dialog with a list of text items
- Using Anko in Views
- Logging using Anko
- Handling dimensions with Anko
- Version checking on Android
-
Anko Layouts
- Introduction
- Setting up Anko library for Anko layouts in Gradle
- Creating user-interface programmatically
- Working with the old code of XML layouts
- Using the provided AnkoComponent interface
- Setting theme for Android views in Anko
- Setting layout parameters for Anko views
- Adding listeners to Anko views
- Inserting XML layouts into DSL
- Converting XML files into DSL
- Showing Snackbar
- Showing Toasts
- Accessing views using synthetic properties
- Accessing views of view groups using extension functions
-
Databases and Dependency Injection
- Introduction
- Using SQLite database in Kotlin
- Creating database tables
- Injecting dependencies in Kotlin
- Reading data from database
- Converting database cursor into list of objects
- Using parseOpt for nullable object
- Inserting data into database
- Creating singletons in Kotlin
- Using Dagger 2 with Kotlin
- Using Butterknife with Kotlin
-
Networking and Concurrency
- Introduction
- How to fetch data over network
- How to create data class in Kotlin
- How to copy data class with modifications
- How to parse JSON data from network to data class
- How to download a file in Kotlin
- How to use RxJava and Retrofit with Kotlin
- How to make an endless list using RecyclerView
- How to use Anko to run background tasks with Kotlin in Android
- How to use coroutines to achieve multithreading
- Lambdas and Delegates
- Testing
- Web Services with Kotlin
- Other Books You May Enjoy
Product information
- Title: Kotlin Programming Cookbook
- Author(s):
- Release date: January 2018
- Publisher(s): Packt Publishing
- ISBN: 9781788472142
You might also like
book
Kotlin Standard Library Cookbook
Build optimized applications in Kotlin by learning how to smartly implement standard libraries. Key Features Get …
book
Kotlin Programming By Example
Enhance your Kotlin programming skills by building 3 real-world applications About This Book Build three full-fledged, …
book
Kotlin Cookbook
Use Kotlin to build Android apps, web applications, and more—while you learn the nuances of this …
book
Reactive Programming in Kotlin
Learn how to implement Reactive Programming paradigms with Kotlin, and apply them to web programming with …