Wednesday 24 May 2017

Kotlin android Examples

Kotlin android

Kotlin an official language on Android

Kotlin android

Kotlin




Normally we will create the native android applications in java programming language.



When run we run the java application, the application compiled into bytecode contains set of instructions and runs in the virtual machine.



JetBrains is a software development company. Who developed the android studio based on IntelliJ.

Now JetBrains introduced the Kotlin language.

Kotlin is programming language runs on the JVM.


Why Kotlin For Android?

Kotlin is very easy to start because it works side by side with Java and C++ on Android.So you can keep your existing code, continue to use the various Android libraries, and incrementally add Kotlin code to your project

Kotlin reduces the amount of boilerplate code.

you can call into the Java language from Kotlin, and you can call into Kotlin from the Java language.

Kotlin is expressive, concise, extensible, powerful, and a joy to read and write.

Kotlin has safety features like nullability and immutability to increase the performance and health of the android application.

install the kotlin plugin:

Kotlin plugin is inbuilt in android studio 3.0 version,
For the earlier version, you can use following steps to install the kotlin plugin in your android studio.

1.open your android studio.
go to File under files, select Settings.
Kotlin android
File > Settings

2.In Settings, you can see the many items like Appearance, Editor, Plugins, version control and etc in these select Plugins.
Kotlin android
plugins

3.Under plugin, you can see the Install JetBrains plugin at the bottom of the window.
select Install JetBrains plugin.
In the Browse JetBrains plugin window, search for the kotlin.
Kotlin android
Kotlin

4.Install the kotin plugin.
Once the installation completed it will ask you to restart the android studio.

Kotlin android
Restart
The kotlin plugin installed in your android studio, Now you can develop the android application with kotlin language.

Please check this installation of kotlin plugin in android studio.


First Kotlin android applicattion

Below, I am going to show you how to develop kotlin android application in android studio.

1. open android studio and create new project.
Kotlin android
Create new project

After entering the Application and company domain click Next,
In my case I have named the application name as FirstKotlinApp.

Then, Select for what purpose you are going to develop this application like Mobile and Tablet and wear or TV.
Please select Mobile and Tablet , press Next,
Kotlin android
Mobile and Tablet
Create new Blank Activity to start the app as a Normal blank activity,
Click Next,
Kotlin android
Blank Activity
In this step It will ask you to configure the Activity Name and Layout Name, In my project I set the default name for both activity and layout.
Then click finish.
Kotlin android
Activity Name
Now you can see the default java android project be created .

You need to convert the Java project in to kotlin project.
You can also do convert kotlin to  java project.

To convert the java langage to kotlin , you need to "Enter Action" in android studio.

Press "ctrl + shift + a" to open the Enter action tool.

Kotlin android
Enter action
In the enter action search for "Convert Java File to Kotlin File" then press enter to convert the java files into kotlin files.
Now you can see the Java file is converted into kotlin file. the extension of the file chnaged from .java to .kt

After converting the java files into kotlin you cannot directly edit the code. still kotlin not configured.
You need to configure the kotlin in the gradle build system.
Kotlin not configured
select the config > android with gradle to add the kotlin into the gradle.

Kotlin android

Adding the kotlin into gradle make the changes in the gradle file.
Kotlin android


Once added the kotlin into the gradle in will ask you the sync the project.


Kotlin android
Sync
Once the sync is done , Now you application is ready to run.

You can't see any difference in java language android application and kotlin language android application UI.
It will be in the speed and processing.

MainActivty.Kt 

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

    }
}


Screenshot of the FirstKotlinApp



No comments: