Kotlin Android basic fundamental codes

Kotlin is now an official language on Android. Everyone wants to start the android app in kotlin because 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.

Kotlin Android basic fundamental codes

If your Android application in Java you can also convert it 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 important change required when you are building an Android app in kotlin. Also, we are comparing it with the java language 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

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()
    }

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()
        }

4. Extend class in kotlin

That’s little different, must remember.

 class MainActivity : AppCompatActivity() {}

The Java equivalent would be:

public class MainActivity extends AppCompatActivity {}

5. Override method (in Kotlin its fun)

Easy syntax in kotlin, remember it.

override fun onCreate(savedInstanceState: Bundle?) {

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

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 colors 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!"

      }
  }

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.

java convert to kotlin

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

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


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.