Kotlin Programming Cookbook

Discover android programming and web development by understanding the concepts of Kotlin Programming
Preview in Mapt

Kotlin Programming Cookbook

Aanand Shekhar Roy, Rashi Karanpuria

Discover android programming and web development by understanding the concepts of Kotlin Programming

Quick links: > What will you learn?> Table of content

Mapt Subscription
FREE
$29.99/m after trial
eBook
$28.00
RRP $39.99
Save 29%
Print + eBook
$49.99
RRP $49.99
What do I get with a Mapt Pro subscription?
  • Unlimited access to all Packt’s 5,000+ eBooks and Videos
  • Early Access content, Progress Tracking, and Assessments
  • 1 Free eBook or Video to download and keep every month after trial
What do I get with an eBook?
  • Download this book in EPUB, PDF, MOBI formats
  • DRM FREE - read and interact with your content when you want, where you want, and how you want
  • Access this title in the Mapt reader
What do I get with Print & eBook?
  • Get a paperback copy of the book delivered to you
  • Download this book in EPUB, PDF, MOBI formats
  • DRM FREE - read and interact with your content when you want, where you want, and how you want
  • Access this title in the Mapt reader
What do I get with a Video?
  • Download this Video course in MP4 format
  • DRM FREE - read and interact with your content when you want, where you want, and how you want
  • Access this title in the Mapt reader
$0.00
$28.00
$49.99
$29.99 p/m after trial
RRP $39.99
RRP $49.99
Subscription
eBook
Print + eBook
Start 14 Day Trial

Frequently bought together


Kotlin Programming Cookbook Book Cover
Kotlin Programming Cookbook
$ 39.99
$ 28.00
Teradata Cookbook Book Cover
Teradata Cookbook
$ 35.99
$ 25.20
Buy 2 for $35.00
Save $40.98
Add to Cart

Book Details

ISBN 139781788472142
Paperback434 pages

Book Description

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.

Table of Contents

Chapter 1: 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
Chapter 2: 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
Chapter 3: 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
Chapter 4: 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
Chapter 5: 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
Chapter 6: 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
Chapter 7: 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
Chapter 8: 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
Chapter 9: 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
Chapter 10: 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
Chapter 11: 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
Chapter 12: Lambdas and Delegates
Introduction
Click listeners using lambdas
Using lazy delegate in Kotlin
Using the observable delegate
Using vetoable delegate
Writing your own delegates
Using the lateinit modifier
Working with SharedPreferences
Creating a chain of multiple lets in Kotlin
Creating global variables
Chapter 13: Testing
Introduction
Unit testing Kotlin code
Unit testing with Mockito
Running instrumentation tests
Writing JUnit rules in Kotlin (@Rule)
Acceptance tests using Espresso Kotlin
Writing assertEquals in Kotlin
Chapter 14: Web Services with Kotlin
Introduction
How to run the application on Tomcat
Setting up dependencies for building RESTful services
How to create a REST controller
Creating the Application class for Spring Boot

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.

Authors

Table of Contents

Chapter 1: 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
Chapter 2: 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
Chapter 3: 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
Chapter 4: 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
Chapter 5: 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
Chapter 6: 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
Chapter 7: 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
Chapter 8: 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
Chapter 9: 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
Chapter 10: 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
Chapter 11: 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
Chapter 12: Lambdas and Delegates
Introduction
Click listeners using lambdas
Using lazy delegate in Kotlin
Using the observable delegate
Using vetoable delegate
Writing your own delegates
Using the lateinit modifier
Working with SharedPreferences
Creating a chain of multiple lets in Kotlin
Creating global variables
Chapter 13: Testing
Introduction
Unit testing Kotlin code
Unit testing with Mockito
Running instrumentation tests
Writing JUnit rules in Kotlin (@Rule)
Acceptance tests using Espresso Kotlin
Writing assertEquals in Kotlin
Chapter 14: Web Services with Kotlin
Introduction
How to run the application on Tomcat
Setting up dependencies for building RESTful services
How to create a REST controller
Creating the Application class for Spring Boot

Book Details

ISBN 139781788472142
Paperback434 pages
Read More

Read More Reviews

Recommended for You

Teradata Cookbook Book Cover
Teradata Cookbook
$ 35.99
$ 25.20
Hands-On Microservices with Kotlin Book Cover
Hands-On Microservices with Kotlin
$ 35.99
$ 25.20
Python Web Scraping Cookbook Book Cover
Python Web Scraping Cookbook
$ 31.99
$ 6.40
Programming Concepts in Python [Video] Book Cover
Programming Concepts in Python [Video]
$ 124.99
$ 106.25
Reactive Programming in Kotlin Book Cover
Reactive Programming in Kotlin
$ 35.99
$ 25.20
IPython Interactive Computing and Visualization Cookbook - Second Edition Book Cover
IPython Interactive Computing and Visualization Cookbook - Second Edition
$ 27.99
$ 19.60