Getting Started With Android Using Kotlin : A Kickstarter Guide

Getting Started With Android & Kotlin : A Kickstarter Guide

We have covered the Kotlin basics in our previous articles

1. Getting Started With Kotlin Language : A Beginners Guide

2. Android Developers Have You Switched To Kotlin Yet ?

Before we jump into creating our first kotlin based android project let us quickly go through some facts about it :

Kotlin Quick Facts :

  • Kotlin does not require a ; after each statement
  • Kotlin is null safe
  • Kotlin offers 100% Java interoperability
  • Kotlin has no primitives (but makes use of them internally on the JVM, if possible, to improve performance)
  • Kotlin classes have properties, no simple fields
  • Kotlin offers data classes, with automatically generated equals/hashCodemethods, getters & setters and a copy method
  • Kotlin only knows about runtime exceptions, no checked exceptions
  • Kotlin has no new. Creating objects is done just by calling their constructor like any other method
  • Kotlin supports (sensibly restricted) operator overloading. For example, you can access a value in a map like this: val a = someMap["key"]
  • Kotlin can not only be compiled to JVM byte code, but also to Java Script, enabling developers to write both backend and frontend code in Kotlin
  • Kotlin is fully compatible to Java 6, which is especially interesting on Android, which only supports Java up to version 6 on most common devices
  • Kotlin is an officially supported language for Android development
  • Kotlin distinguishes between mutable and immutable collections.
  • Kotlin offers coroutines
  • Kotlin is open source and Apache License 2.0 licensed
  • Kotlin is developed by JetBrains, the creators of IntelliJ IDEA a powerful and popular IDE for Java and others

In this journey we will cover :

  1. Intsallation
  2. Creating our first Project
  3. Facts About: Why Kotlin ?

1: Installation :

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. To do so, start Android Studio and open Plugins from the Configure menu as shown below:

click the Install JetBrains plugin…​ button. as shown below

In the opened dialog, search for Kotlin, select the corresponding plugin and press Install.

Wohoo! we are set to start writing our first code in Kotlin !

2 Creating Our Maiden Project :

In-order to create our first android project please follow given steps as mentioned in wizard

  1. click File > New > New Project. Or if you’ve just opened Android Studio and see the Welcome to Android Studio window, click Start a new Android Studio project.
  2. On the first screen, check Include Kotlin support.
  3. Click Next and continue through the wizard until you’re done.

When you select an activity template, it will be provided in Kotlin code. Currently, only some the templates for the Phone and Tablet form factor are provided with idiomatic Kotlin code. All your Kotlin files can be found in src/main/java/ directory.

Add Kotlin Support To The Existing Java Project :

If you want to add Kotlin code to an existing project, simply click File > Newand select one of the various Android templates. If you don’t see the list of templates in this menu, first open the Project window and select your app module.

In the wizard that appears, select Kotlin for the Source language.

Continue through the wizard, and you’re done.

Convert Existing Java Code To Kotlin Code :

In Android Studio 3.0, open a Java file and select Code > Convert Java File to Kotlin File.

Or, create a new Kotlin file (File > New > Kotlin File/Class), and then paste your Java code into that file — when prompted, click Yes to convert the code to Kotlin. You can check Don’t show this dialog next time, which makes it easy to dump Java code snippets into your Kotlin files.

Creating New Kotlin File :

To create a new Kotlin file instead of a Java file, simply choose File ▸ Kotlin File/Class from the menu as shown below

Building & Publishing App in Kotlin :

The process of building and packaging kotlin app for release is similar to what you used to do in java. Kotlin compiler produces byte-code like you do in java and the final app is similar to the app built in java.

Let’s see how to call android API’s in kotlin : (Source )

1. Declaring Activity :

In Kotlin :

class MyActivity : AppCompatActivity() {
  override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity)
  }
}

In Java :

public class MyActivity extends AppCompatActivity {
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity);
  }
}

On-click Listener :

In Kotlin :

val fab = findViewById(R.id.fab) as FloatingActionButton
fab.setOnClickListener {
  ...
}

In Java :

FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
  @Override
  public void onClick(View view) {
    ...
  }
});

Item click listener :

In Kotlin :

private val mOnNavigationItemSelectedListener
    = BottomNavigationView.OnNavigationItemSelectedListener { item ->
  when (item.itemId) {
    R.id.navigation_home -> {
      mTextMessage.setText(R.string.title_home)
      return@OnNavigationItemSelectedListener true
    }
    R.id.navigation_dashboard -> {
      mTextMessage.setText(R.string.title_dashboard)
      return@OnNavigationItemSelectedListener true
    }
 }
 false
}

In Java:

private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
    = new BottomNavigationView.OnNavigationItemSelectedListener() {
  @Override
  public boolean onNavigationItemSelected(@NonNull MenuItem item) {
    switch (item.getItemId()) {
      case R.id.navigation_home:
        mTextMessage.setText(R.string.title_home);
        return true;
      case R.id.navigation_dashboard:
        mTextMessage.setText(R.string.title_dashboard);
        return true;
    }
    return false;
  }
};

3. Facts About: Why Kotlin ?

Why you should make kotlin you next preferred mobile app development platform has been beautifully compiled by PROGRAMIZ in the given image below :

Source : Richard Darell

References For Learning :

  1. https://developer.android.com/kotlin/get-started.html
  2. http://kotlinlang.org/docs/tutorials/kotlin-android.html
  3. http://www.vogella.com/tutorials/AndroidKotlin/article.html
  4. https://www.udemy.com/the-complete-kotlin-developer-course/

What’s Next : We will develop one small application to elaborately understand the kotlin based android app development.

Pramod Chandrayan

Founder & CEO (Mobibit Soft (P) Ltd) | Mobile App Development Consultant | Startup Mentor | Spiritual Seeker

More Posts - Website

Follow Me:
TwitterFacebookLinkedInGoogle Plus

Related posts

Leave a Comment