Kotlin Android Studio 3.0 Development Essentials

My notes from this book:

Notes

Android Architecture

Android_Framework

  1. Linux Kernel - provides a level of abstraction between the device hardware and the upper layers of the Android software stack. The Kernel handles:
    • preemptive multitasking, memory, process, and power management
    • provides network stack and device drivers for display, Wi-Fi and audio
  2. Android Runtime --- ART - When an Android app is built within Android Studio, it is compiled into an intermediate bytecode format (referred to as DEX format). ART uses Ahead-of-Time (AOT) process to translate the bytecode down to the native instructions required by the device processor - down to Executable and Linkable Format (ELF)
  3. Each time the application is subsequently launced, the ELF executable version is run, resulting in a faster application performance and improved battery life

Application Framework

  1. Application Framework is a set of services that collectively form the environment where Android applications run and are managed.
    • Activity Manager - controls all aspects of the application lifecycle and activiy stack
    • Content Providers - Allow applications to publish and share data with other applications
    • Resource Manager - Provides access to non-code embedded resources such as strings, color settings, and user interface layouts
    • Notifications Manager - Allow applications to display alerts and notificaitons to the user
    • View System - An extensible set of views used to create application user interfaces
    • Package Manager - System where applications are able to find out information about other apps currently installed on device
    • Telephony Manager - telephony services available on device such as status and subscriber info
    • Location Manager - allows app to receive updates about location changes

Application Anatomy

  1. Activities - is a single, standalone module of application functionality that usually correlates directly to a single user interface screen and its funcitonality
    • Activities are fully reuseable and interchangeable building blocks that can be shared amongst different applications
  2. Android Intents - Intents are the mechanism where one activity is able to launch another and implement the flow through the acitivities that make up an application
    • made up of description of the operation to be performed and, optionally, the data on which it is to be performed
    • intents can be explicit or implicit (implicit uses Intent Resolution)
  3. Broadcast Receivers - listeners using Intent Filters
  4. Android Services - run in background and have no user interface. Can be started and managed from activities

Kotlin

  1. Language, compiler, and related tools are all open source and available for free under the Apache 2 license
  2. Goals of Kotlin are to make code concise, safe, and interoperable with Java
  3. Try Kotlin - Playground

Data Types

  1. Number - val mynumber = 10
  2. Letter - val myletter = 'c'
  3. Integers
    • All integers in Kotlin are signed (positive, negative, and zero values)
    • supports 8, 16, 32, 64 bit integers (Byte, Short, Int, Long respectively)
  4. Floating Point Data Types
    • decimals
    • float (32-bit)
    • double (64-bit)
  5. Boolean
    • true or false (1 or 0)
  6. Character
    • Char is used to store a single character of rendered text such as a letter, numerical digit, punctuation mark or symbol
    • Internally, characters in Kotlin are stored in the form of 16-bit Unicode grapheme clusters
    • Grapheme Cluster - is made of two or more Unicode code points that are combined to represent a single visible character
    • Use single quotes to assign a Char
  7. String
    • sequence of characters that typically make up a word or sentence
    • double quotes
    • triple quotes for multi-line strings
    • leading spaces on each line can be removed by making a call to trimMargin() function
  8. Interpolation
    • "$variable is inserted into this string"
    • ${maxcount - inboxCount} --- math in strings
  9. Escape Sequences
    • \n - newling
    • \r - carriage return
    • \t - horizontal tab
    • \\ - backslash
    • \" - Double quote (used when placing a double quote into a string declaration)
    • \' - single quote
    • \$ - dollar sign
    • \unnnn - Double byte Unicode scalar where nnnn is replaced by four hexadecimal digits representing the Unicode character
  10. Mutable Variables - changeable
  11. Immutable Variables - constants
  12. var - is used to declare a variable
  13. If no initial value then you have to provide a type, example:
    • var userCount: Int
  14. val - immutable values are defined with this
  15. Use Immutable variables whereever possible
  16. All properties and functions provided by Kotlin can be found here:

Type Annotations and Type Interface

  1. Kotlin is statically typed
  2. val userCount: Int = 10
  3. Nullable Type - The purpose of nullable types is to provide a safe and consistent approach to handling situations where a variable may have a null value assigned to it
  4. To do this, simply:
    • val username: String? = null
  5. Elvis Operator
if (myString != null) {
    return myString
} else {
    return "String is null"
}

is the same as

return myString ?: "String is null"

Type Casting

  1. Letting the compiler know the type of object that is expected is know as type casting - using the keyword as

Functions

  1. To accept a variable number of arguments into a function
    • fun displayStrings(vararg strings: String)

Lambda Expressions

  1. Lambda is a self-contained block of code
    • val sayHello = { println("Hello") }
    • sayHello()
  2. A useful feature of lambdas and the ability to create function references is that they can be both passed to functions as arguments and returned as results

Higher-order functions

  1. hof - a function that is capable of receiving a function or lambda as an argument, or returning one as a result is referred to as a higher-order function

Classes

In addition to primary and secondary constructors, a class may also contain initializer blocks which are called after the constructors

Activity Lifecycle

activity_lifecycle

Bundle class - provides a container for storing data using a key-value pair mechanism
- key - takes the form of string values
- values - can be in the form of a primitive value or any object that implements the Android Parcelable interface
- Custom classes can be made "parcelable" by implementing the set of methods defined in the Parcelable interface, details are found here:
- Android Documentation

Views, View Groups, and Layouts

  1. Every item in a user interface is a subclass of the Android View class, android.view.View
  2. A view can also be comprised of multiple other views (otherwise known as a composite view)
  3. These composites are subclassed form the Android ViewGroup class (_android.view.ViewGroup)

Layout Managers

  1. Layouts are container views designed for the sole purpose of controlling how child views are positioned on the screen:
    • ConstraintLayout - positioning and behavior of the views in a layout to be defined by simple constraint settings assigned to each child view
    • LinearLayout
    • TableLayout
    • FrameLayout
    • RelativeLayout
    • AbsoluteLayout
    • GridLayout
    • CoordinatorLayout

ConstraintLayout

  1. Constraints - a set of rules that dictate the way a widget is aligned and distanced in relation to other widgets, the sides of the containing layout and guidelines
  2. Margins - a form of constraint that specifies a fixed distance
  3. Opposing Constraints - two constraints operating along the same axis on a single widget
  4. Constraint Bias - to allow for adjustment of widget position in the case of opposing constraints
  5. Chains - provide a way for the layout behavior of two or more widgets to be defined as a group, chains can be declared either on the vertical or horizontal axis
  6. Chain Styles Spread chain, spread inside chain, weighted chain, packed chain

Random Notes

  1. to get a hook into a ui:
    • val textBox: EditText = findViewById(r.id.editText)
    • using Kotlin there is an easier way
      • import kotlinx.android.synthetic.main.content_state_change.*
      • val userText = textBox.text
Kotlin Android Studio 3.0 Development Essentials
Share this