Kotlin Integration with Android SDK (KAD 05)

Source: Internet
Author: User

Antonio Leiva

Time:Dec,

Original link:https://antonioleiva.com/kotlin-integrations-android-sdk/

Using the Kotlin language not only simplifies your code, but it also simplifies calling Java code from Kotlin.

How does this work? Simply using some common structure can get a sense of kotlin.

In this article, you'll see some examples of how to promote the same code. It also prevents the Java code from including the template part.

Kotlin Integration with Android SDK

As the Android framework is based on the Java library, everything we mentioned before about Java applications is fully applicable to Android.

Let's take a look at some examples.

Getter and Setter methods mapped to attributes

In the previous articles, we have seen getter and setter. That's just an explanation.

As we discussed, Kotlin uses properties instead of field +getter+setter to assign values in the same way as get in the Java public domain.

But we also know that simple assignments can execute code, or custom actions that we implement.

The rules of this type are any setx and Getx found in Java are mapped to Kotlin x.

For example, if you are using TextView, you can set text with a statement that resembles a property:

1 val textview:textview = ... 2 textview.text = "My text"

Of course, this can be applied to any class. In the same way, you can access (for example) ApplicationContext or Layoutinflater in the activity:

1 Layoutinflater.inflate (R.layout.view_item, parent) 2 val hello = applicationcontext.getstring (R.string.hello)

This does not have to worry about performance, they are in fact mapped to the original getter and setter methods, so it is the same as calling them directly.

interface methods map to lambda expressions

It's amazing. For any simple thing about Android, do you want to create an anonymous class? Instead of using Kotlin, you do not need to do this for an Android listener.

The condition is this: an interface is required for a method. A typical example is the Setonclicklistener of the view class.

You can make a simple sentence like this:

1 view.setonclicklistener {navigatetonextactivity ()}

How easy is it to see?

If you need to return the view from the original interface, you can also overwrite it:

1 View.setonclicklistener {v-navigatewithview (v)}

Description: What is this strange structure? It is a lambda expression that indicates that the input parameter of the function is the method defined on the left side of the arrow. The right side of the arrow is the function body and the previous line is the result. We'll discuss these in a later article.

Conclusion

In the Kotlin project, it is easy to integrate other Java libraries, especially the Android framework.

If you want to learn more about Kolin, please join this list and you will be able to receive these articles in your Inbox.

Kotlin Integration with Android SDK (KAD 05)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.