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

Kotlin for Android

Kotlin for Android

A talk presented at GDG Uyo meetup on January 20th 2018 on the basic features of Kotlin in Android development.

Idorenyin Obong

January 20, 2018
Tweet

More Decks by Idorenyin Obong

Other Decks in Technology

Transcript

  1. • Developed by JetBrains JetBrains is a software development company

    whose tools are targeted towards software developers and project managers
  2. • Runs on JVM . JVM - Java virtual machine

    is an abstract computing machine that enables a computer to run a Java program.
  3. • It is open source - first commit on the

    project was in 2010 (https://github.com/JetBrains/kotlin)
  4. 1. No “new” keyword // Java ArrayList<String> arrayList = new

    ArrayList<>(); // Kotlin var arrayList: ArrayList<String> = ArrayList()
  5. 2. Smart inference just like JavaScript ! // Java String

    string = "Hello world"; // Kotlin var arrayList = "Hello world"
  6. 3. Null safety // Java - no compiler error String

    h = null; // Kotlin - compiler error var h:String = null
  7. 3. Null safety // Kotlin - no compiler error var

    h:String? = null // Kotlin - compiler error on line 2 var h:String? = null var idee = h.length
  8. 3. Null safety // Kotlin - no compiler error var

    h:String? = null // Kotlin - compiler error on line 2 var h:String? = null var idee = h.length // no compiler error var idee = h!!.length
  9. 3. Null safety - class variables // Kotlin - no

    compiler error lateinit var h:String
  10. 4. Mutability & Immutability // Kotlin - cannot be reassignable

    val variable1 = "Hello" // Kotlin - can be reassignable var variable2 = "World"
  11. // Java public class Model { String name; public String

    getName() { return name; } public void setName(String name) { this.name = name; } } 5. Data classes
  12. // Java public class Model { String name; public String

    getName() { return name; } public void setName(String name) { this.name = name; } } // Kotlin data class Model(var name: String, var age: Int) 5. Data classes
  13. 6. View binding // Java @Override protected void onCreate(Bundle savedInstanceState)

    { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); RecyclerView recyclerView = findViewById(R.id.recycler_view); }
  14. 6. View binding // Kotlin app-module build.gradle file: apply plugin:

    'kotlin-android-extensions' // Kotlin MainActivity.kt file: import kotlinx.android.synthetic.main.activity_main.* class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) recycler_view.adapter = adapter } }