Skip to main content

An introduction to Kotlin for Android development

Kotlin for Android development

For years, Java was the one official language for Android development. While other options were available through alternative tools like Unity or Xamarin, Java with the Android SDK was still the clear choice for those wanting to learn Android development the “proper” way. Then came Kotlin for Android.

Also read: I want to develop Android apps – What languages should I learn?

As of 7 May 2019, Kotlin has been the official “preferred” language for Android development according to Google. It has been available as a built-in feature of Android Studio and an official language for Android for longer than that.

So, should you use Kotlin for Android development? How is it different from Java? What is the learning curve like? Let’s find out.

Kotlin for Android development vs Java

Kotlin is similar to Java in many ways and runs on the Java Virtual Machine. It is 100% interoperable with Java, and thus there is not hit in performance when choosing to use Kotlin for Android development, nor increase in file size.

Kotlin Android development

Credit: Adam Sinicki / Android Authority

Kotlin does involve some changes in the way code is handled, however. As a general rule, Kotlin is a little simpler to read and easier to get to grips with as compared with Java. Kotlin requires less “boilerplate code.” This means there is less repetitive code necessary just to implement basic features. In many cases, two lines of Java code can be compressed into a single line of Kotlin.

Kotlin also does away with null pointer exceptions, and even lets you off the hook when it comes to ending lines of code with semi-colons!

For these reasons, Kotlin represents an easier jumping-on point for many new Android developers. And the fact that it is now the preferred option according to Google, means that you can guarantee there will be great support for the language going forward.

Why you might still choose Java

That’s not to say that Kotlin for Android development is perfect.

While Kotlin might be the preferred official language, the fact remains that Java has been around as a primary option for much longer – since way back when most people used Eclipse IDE to make their apps!

What this means is that many of the big apps on the Play Store will be built using Java. There’s not a huge amount of incentive for those developers to rewrite their code in Kotlin, and as such, they are more likely to hire developers that know Java.

Java vs Kotlin for Android develpoment

Not only that, but Java is used much more widely outside of Android development. In fact, Java is regularly listed as one of the most in-demand programming languages for employers, alongside Python. Kotlin is further down the list.

Simply: more people know Java than know Kotlin. And with so Java so heavily entrenched, it may be easier for companies to start new projects in Java too. It certainly makes more sense for a developer to learn Java if they hope to work in the industry and don’t want to focus solely on Android.

An introduction to coding in Kotlin

If you want to have a go at Kotlin development for Android, the good news is that it’s now extremely simple to get set-up. Simply start a new project in Android Studio and choose Kotlin as your preferred language.

Also read: A guide to Android app development for complete beginners in 5 easy steps

When you start that new project, you’ll be greeted with some code by default. This is the code necessary to display “Hello World” on the screen and assign a starting point for your program along with a layout file. In other words, this is the boilerplate we were talking about earlier! Let’s take a look at what’s here and what it can teach us about Kotlin.

First, you may notice that you declare classes using class just as you would in Java. The difference is that there’s no public keyword, which is because all classes in Kotlin are public and final. For those unfamiliar with what this means, it means that other activities outside of this file can access the functions you create. You might also notice that we aren’t using extend to inherit the properties of the superclass, either. Instead, we use a colon which does the same thing.

What about that fun command? This is short for “function”; so instead of writing public void you’ll now write fun. Arguments are specified in brackets following the function name. You’ll also need to know how to define variables, which is a little different in Kotlin for Android development. To create a string, you might write:

var text: String = “Hello”

Kotlin is usually smart enough to identify a variable’s type on its own though. just like Python, so you can generally just write:

var text = “Hello”

To create an integer, you could simply write:

var num = 3

This is how you would create a mutable (changeable) variable, whereas val is used to create constants.

For a much more in-depth introduction to Kotlin syntax and structure, and how it differs from java, check this post:

How Kotlin saves you time and busywork

A lot of the time, code will look a fair bit simpler and shorter in Kotlin for Android as compared with Java. Consider the following example of adding an onClickListener to a floating action button (FAB). Here is how you would do it in Java:

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

And this is the same thing in Kotlin:

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

Like I said: less boilerplate. In fact, Kotlin developers can do away with ever having to write findViewByID again! To try this out, first apply a plugin.

findViewByID in kotlin for android

Add the following to your module-level build.gradle:

apply plugin: ‘kotlin-android-extensions’

Click “sync” and you’ll then be able to import references to your views right at the top of your code, like so:

import kotlinx.android.synthetic.main.<layout>.<view-id>

Now you can access the view directly with no need to use its ID. This makes life much simpler and can save you writing a lot of arbitrary code.

arbitrary code becomes obsolete with kotlin for android


Combining these techniques you can save yourself a whole lot of busywork and you’ll find many more useful time-saving strategies going forward.

Hopefully, this gives you a basic introduction to Kotlin for Android development, and an idea of how it differs from Java. Which is right for you will depend on your personal preferences and goals. But more choice is never a bad thing!



source https://www.androidauthority.com/kotlin-for-android-development-1132127/

Comments

Popular posts from this blog

4 Surprising Ways Artificial Intelligence will Empower Consumers

Ever wonder what makes Siri search the items inside the iPhone and on the web by recognizing your voice? How does Google listening work? Artificial Intelligence and data science have already infused in this consumer-oriented generation, but we have not realized it yet. It has been quite a while that natural language processing, speech recognition, and gesture recognition system took over the market with their all new features, but what are the new things in AI that are yet to come? Few amazing transformations are: Retailing sector to become AI-ready Voice Recognition will change the game Search engines to become smarter Machine learning to bring revolutionary changes Recently we got to learn about a verbal spat between Elon Musk and Mark Zuckerberg over the probable risk and opportunities brought by AI . Believe it or not, but we already accustomed to Artificial Intelligence and probably waiting to witness jaw-dropping inventions. Some of AI inventions we have known till yet are: Mach

How to unhide or show folders in mx player list

In this blog post, I tell you about how to Show or Hide folders from MX player list. There are two methods to Show folder from MX Player list. Method 1: Unhide / show folders If you want to temporarily Show / Unhide hidden folder from MX Player list, then go to Settings and untick Recognize .nomedia ".  Method 2: Permanently unhide / show folder: Open memory by any file explorer and I recommend X-Plore, and open the folder that is Hidden and find the file " .nomedia ". If you didn't find it, you should first enable "Show files hidden files that starts with .(dot)". Delete the file and you just need to refresh MX Player list to take changes. Note: MX Player always hide those folders which file " .nomedia " exists.

How to use arrays in Python

Arrays in Python give you a huge amount of flexibility for storing, organizing, and accessing data. This is crucial, not least because of Python’s popularity for use in data science. But what precisely is an array? And how do you use arrays in Python? Also read: How to use dictionaries in Python Read on, and we’ll shed some light on the matter. What is an array? An array is a way to store multiple values in a single variable. That means that you can use a single “reference” in order to access your data. A list is also an example of a variable that stores multiple values, but has some slight differences. When using lists in Python, you store a series of values each with a numbered index. For example, this is how you would create a list of fruits in Python: fruits = [“apple”, “orange”, “pear”, “nectarine”] If we then say: print(fruits[3]) We will see “nectarine” appear on the screen (the first entry is stored as “0”). Also read: How to use lists in Python This is not an