Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Using Kotlin in Android Studio 3.0 (Part 6 — Final)

DZone's Guide to

Using Kotlin in Android Studio 3.0 (Part 6 — Final)

This series culminates with a guide to lambdas and database management when developing with Kotlin in Android Studio 3.X.

· Mobile Zone ·
Free Resource

Lambdas

Lambdas are one of the most powerful tools in Kotlin since it allows defining an anonymous function and preventing us from having to write the specification of the function in an abstract class or interface, and then the implementation of the class. Let’s look at the following example using anonymous classes:

we first need to create an interface

interface MathOperation{
    fun operation( x:Int,  y:Int):Int
}

and then we write anonymous classes that implement this interface

class MainActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
   super.onCreate(savedInstanceState)
   setContentView(R.layout.activity_main)
   var x:Int = 3
   var y:Int = 3
   var resultOfAddition:Int = performOperation(x,y,
                   //anonymous class
                  object:MathOperation{
                       override fun operation(x: Int, y: Int): Int {
                                   return x+y;
                       }
                  });
   var resultOfSubtraction:Int = performOperation(x,y,
                 //anonymous class
                 object:MathOperation{
                      override fun operation(x: Int, y: Int): Int {
                                   return x-y;
                     }
                 });
   txt.text = "Addition is $resultOfAddition and Subtraction is $resultOfSubtraction"
}

fun performOperation(x:Int, y:Int, opt:MathOperation):Int{
     return opt.operation(x,y)
}
}

Now, we will consider the following example that uses lambda expressions

class MainActivity : AppCompatActivity() {

   override fun onCreate(savedInstanceState: Bundle?) {
     super.onCreate(savedInstanceState)
     setContentView(R.layout.activity_main)
     var x:Int = 3
     var y:Int = 3
     var performOperation = {x+y}
     val resultOfAddition = performOperation()
     performOperation = {x-y}
     val resultOfSubtraction = performOperation()
     txt.text = "Addition is $resultOfAddition and Subtraction is $resultOfSubtraction"
  }
}

Clearly, with lambdas we can simplify our code a lot.

Database Management

As you may know, Android uses SQLite as a database management system. SQLite is a relational database management system (RDBMS). If most RDBMSs such as MySQL, Oracle, etc. are standalone server processes, then SQLite is embedded because it is provided in the form of a library that is linked in applications.

If you have never created a database Android application before, you can refer to the my article (and get Java code). You can create a database Android application with Kotlin classes are used in this application like Java classes are used in my article but there are some changes (and source code, of course) as follows:

  • Student.java  -> Student.kt

  • MyDBHandler.java -> MyDBHandler.kt

  • MainActivity.java -> MainActivity.kt

And you can create a Kotlin class by selecting the package that will contain this class, right clicking and choosing New > Kotlin File/Class:

Image title

Fill in Name and change it from File to Class for Kind:

Image title

You can get the completed Kotlin source here.

Conclusion

This is the last article of my series (part 1, part 2, part 3, part 4, part 5, and this part). I hope that my series will help your familiarity with Kotlin and how to use it in the Android Studio 3.0 environment (or 3.X). 

Topics:
kotlin ,android ,mobile ,mobile app development ,tutorial

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}