Hey Kotlin, Its my first example!

Pratik Butani
Android School
Published in
3 min readJun 14, 2017

--

Android and Kotlin

Android is now officially supporting the Kotlin programming language, in addition to the Java language and C++. Kotlin is a new programming language from JetBrains, the maker of the world’s best IDEs.

I learned a lots of things from Why Kotlin is my next programming language by Mike Hearn, Thank you Mike.

Today I started development of Kotlin, just to show “Hello Kotlin!”. I want to share demo and prerequisites so it can be useful to new developers of Kotlin.

Installing the Kotlin plugin:

  • The Kotlin plugin is bundled with Android Studio starting from version 3.0. If you use an earlier version, you’ll need to install the Kotlin plugin.
  • Go to File | Settings | Plugins | Install JetBrains plugin… and then search for and install Kotlin.
  • If you are looking at the “Welcome to Android Studio” screen, choose Configure | Plugins | Install JetBrains plugin… You’ll need to restart the IDE after this completes.

Creating a Project:

  • It’s extremely easy to start using Kotlin for Android development. In this tutorial we’ll follow the warming up process with Android Studio. If using Intellij IDEA with Android, the process is almost the same.
  • First let’s create a new project. Choose Start a new Android Studio project or File | New project.
Creating Project for Kotlin
  • You have to check option include Kotlin support on this screen.
  • Other steps are same as we are creating projects in Android Studio.

Lets have a look on MainActivity.kt:

class MainActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val toolbar = findViewById(R.id.toolbar) as Toolbar
setSupportActionBar(toolbar)

val fab = findViewById(R.id.fab) as FloatingActionButton
fab.setOnClickListener { view ->
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show()
}
}

override fun onCreateOptionsMenu(menu: Menu): Boolean {
// Inflate the menu; this adds items to the action bar if it is present.
menuInflater
.inflate(R.menu.menu_main, menu)
return true
}

override fun onOptionsItemSelected(item: MenuItem): Boolean {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
val id = item.itemId

if (id == R.id.action_settings) {
return true
}

return super.onOptionsItemSelected(item)
}
}

There will be no any code changes in XML. :) :)

You may waiting for output:

Output: Hello Kotlin Demo

You can download demo from github.

Hope you like it, love it.

I am eager to learn more about Kotlin, I will share whatever I learn.

If its helped you, please help others by clicking that ❤ heart below — Thanks!

--

--

Pratik Butani
Android School

55k+ Reputation Holder on Stack Overflow | #FlutterDev & #AndroidDev | Team Lead @7Span | Contributor | Managing @androidschool