Kotlin is now an official language on Android. Everyone wants to start the android app in kotlin because of it’s Modern, Expressive, safe and less code. Here we are sharing some change need to know before start developing an android application in Kotlin. It’s a very easy Kotlin Android basic fundamental, but yes you have to do practice with us.

If your Android application in Java you can also convert its into Kotlin. Read the post and get know change Java to Kotlin

Top Kotlin Android basic fundamental

Here we are sharing a basic and most improatent change requied, when you are building a Android app in kotlin. Also we are comparing it with java langauge syntax of android.

1. get the Text from EditText

In Kotlin calling .text on your, EditText is fine no need to do getText or toString

Even don’t need findViewById, just this code fine

val name = eda.text 1 val name = eda . text

2. onClickListener on frgament

val view = inflater!!.inflate(R.layout.fragment_sender, container, false) view.button?.setOnClickListener { Toast.makeText(activity,view.editText.text, Toast.LENGTH_LONG).show() } 1 2 3 4 val view = inflater ! ! . inflate ( R . layout . fragment_sender , container , false ) view . button ? . setOnClickListener { Toast . makeText ( activity , view . editText . text , Toast . LENGTH_LONG ) . show ( ) }

3. onClickListener on Activity

It’s more easy in kotlin no need inner method.

button?.setOnClickListener { Toast.makeText(this,editText.text, Toast.LENGTH_LONG).show() } 1 2 3 button ? . setOnClickListener { Toast . makeText ( this , editText . text , Toast . LENGTH_LONG ) . show ( ) }

4. Extend class in kotlin

That’s little different, must remember.

class MainActivity : AppCompatActivity() {} 1 class MainActivity : AppCompatActivity ( ) { }

The Java equivalent would be:

public class MainActivity extends AppCompatActivity {} 1 public class MainActivity extends AppCompatActivity { }

5. Override method (in Kotlin its fun)

Easy syntax in kotlin, remember it.

override fun onCreate(savedInstanceState: Bundle?) { 1 override fun onCreate ( savedInstanceState : Bundle ? ) {

This is the opposite of Java, where type comes before name:

public void onCreate(Bundle savedInstanceState) 1 public void onCreate ( Bundle savedInstanceState )

In Kotlin you don’t need to finish your lines with semicolons, hence the absence of colons in the above snippet. You can add colons if you really want to, but your code will be cleaner and easier to read without them

6. Say goodbye to findViewById

Yes you read right no need more findViewByID , no more confused about widget id.

import kotlinx.android.synthetic.main.content_main.* class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) // No need to call findViewById(R.id.textView) as TextView textView.text = "Kotlin for Android rocks!" } } 1 2 3 4 5 6 7 8 9 10 11 12 import kotlinx . android . synthetic . main . content_main . * class MainActivity : AppCompatActivity ( ) { override fun onCreate ( savedInstanceState : Bundle ? ) { super . onCreate ( savedInstanceState ) setContentView ( R . layout . activity_main ) // No need to call findViewById(R.id.textView) as TextView textView . text = "Kotlin for Android rocks!" } }

Everyone has knew about Java code , so we are not adding java code snip.

Note : Android studio provide support to convert existing code or project convert into Kotlin.

Android Studio 3.0 provides tools to help you start using Kotlin. Convert entire Java files or convert code snippets on the fly when you paste Java code into a Kotlin file.

Source : https://developer.android.com/kotlin/

For example of first Android kotlin project (app development) follow this tutorial – Build Your First Android App in Kotlin

Degree in Computer Science and Engineer: App Developer and has multiple Programming languages experience. Enthusiasm for technology & like learning technical.

Share this: Facebook

WhatsApp

LinkedIn

More

Twitter

Print



Reddit

Tumblr



Pinterest

Pocket



Telegram

Skype



Email

