Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Kotlin for android development

Kotlin for android development

In this deck, I explain the basic concepts and syntax of Kotlin language along with basic concepts of android and how you can combine them to make beautiful apps.

Kashif Mehmood

December 19, 2021
Tweet

More Decks by Kashif Mehmood

Other Decks in Programming

Transcript

  1. Designed by Jetbrains Official language of Android since 2017 Android

    is Kotlin first since 2018 Jetpack Compose in stable since 2021 Kotlin multiplatform recommended for multiplatform mobile applications
  2. This is JAVA • public class GFG • { •

    // M et hod to find an equal index • static int findIndex(String str) • { • int len = st r.length(); • int open[] = new int[len+1]; • int close[] = new int[len+1]; • int index = -1; • • open[0] = 0; • close[len] = 0; • if (str.charAt (0)=='(') • open[1] = 1; • if (str.charAt (len-1) == ')') • close[len-1] = 1; • • // St ore t he number of opening bracket s • // at each index • for (int i = 1; i < len; i++) • { • if ( st r.charAt (i) == '(' ) • open[i+1] = open[i] + 1; • else • open[i+1] = open[i]; • } •
  3. This is Still JAVA • // St ore t he

    number of closing bracket s • // at each index • for (int i = len-2; i >= 0; i--) • { • if ( st r.charAt (i) == ')' ) • close[i] = close[i+1] + 1; • else • close[i] = close[i+1]; • } • • // check if t here is no opening or closing • // bracket s • if (open[len] == 0) • return len; • if (close[0] == 0) • return 0; • • // check if t here is any index at w hich • // bot h bracket s are equal • for (int i=0; i<=len; i++) • if (open[i] == close[i]) • index = i; • • return index; • } •
  4. Ok this is last for JAVA • // Driver Method

    • public static void main(String[] args) • { • String str = "(()))(()()())))"; • System.out.println(findIndex(str)); • } • }
  5. This is Kotlin • fun main() { • val str

    = "()))(()" • • str.forEachIndexed{index , _ -> • val str1= str.substring(0..index+1) • val str2= str.substring(index..str.length-1) • • if(str1.count{it.equals('(')} == str2.count{it.equals(')')}){ • • println("equal at ${index+1}") • return • } • • • } • println("not found") • • }
  6. Kotlin Syntax No need For Semicolons; Variables: Variables are declared

    with var keyword. var name = "Kashif Mehmood" var profession = "Software Engineer" Values / Constants: Values that do not change or immutable are declared with val keyword or const val keyword. val datoOfBirth = "15-03-1998" const val gender = "Male" Type Inference: No need to specify types, types are infered by compiler.
  7. Kotlin Code Structure • Val • Var • Lateinit var

    • Const • Extension functions • Getter • Setter
  8. Const vs Val Val Val are runtime constansts, which means

    that their values can be set at run time. However, once a values is set it can not be modified. Const Consts are compile time constants. Their value has to be assigned during compile time. They can not be assigned to any function or constructors.
  9. Lateinit var VS by Lazy Lateinit var • Use it

    with mutable variable [var] • Allowed with only non- nullable data types • It is a promise to compiler that the value will be initialized in future By lazy • Lazy initialization was designed to prevent unnecessary initialization of objects. • Your variable will not be initialized unless you use it. • It is initialized only once. Next time when you use it, you get the value from cache memory.
  10. Kotlin Operators • Like anyother programming language kotlin provides different

    arithematic operators, comparison operators, assignment, increment decrement operators. • Kotlin also supports operator overloading
  11. Kotlin Control flow • For • While • Do-While •

    Filter • Foreach • For Each Index • Map • Flatmap
  12. Functions In Kotlin Functions are declared using fun keyword Value

    Returning Functions: fun myfunction(param1: String, param2 : Int): String{ return "$param1 $param2" } Non Value Functions: fun myfunction(param1: String, param2 : Int){ print( "$param1 $param2") }
  13. Functions In Kotlin Functions are declared using fun keyword Value

    Returning Functions: fun myfunction(param1: String, param2 : Int): String{ return "$param1 $param2" } Non Value Functions: fun myfunction(param1: String, param2 : Int){ print( "$param1 $param2") }
  14. Views And View Groups Views are UI Widgets like buttons,

    images, textviews View Groups hold views & view groups & position them
  15. Commonly used layout - Constraint layout Each view should have

    at least one horizontal and vertical constraint Used to achieve flat layout hierarchy
  16. Fragments • Fragment represents a reusable portion of the app’s

    user interface. • Each has its own layout & lifecycle. • Cannot live on their own and must be hosted in another activity or fragment.
  17. Fragment Communication • Fragments communicate through view models • Movement

    between fragments with navigation component • Single Activity architecture is preferred