Talk is cheap, let’s code
The previous article introduced the basics of Kotlin, but knowledge alone is not enough. The most important thing is to be able to use Kotlin in real life, which is worth learning. Here’s a simple Android application using pure Kotlin to show you how to use Kotlin in a real project.
Follow Kotlin’s path
At the end of the previous article, we showed how to create a Kotlin-based Android app, but it was too simple because the code was changed to Kotlin and the layout was still XML, which didn’t use all of Kotlin. In order to facilitate the development of Android applications and take advantage of the great advantages of Kotlin, JetBrains has also released a supporting library for Android development, Anko. Its biggest advantage is to create THE UI in the way of DSL. Let’s introduce Anko.
Anko
What is Anko and why is it used
The purpose of the Anko library is to improve the efficiency of Android development using the advantages of the Kotlin language. There are four main sections: Anko Commons, Anko Layouts, Anko SQLite, and Anko Coroutines. You can also see the official wiki for all four sections. In fact, the biggest change is the layout, regular Android projects, we generally use XML to write layout XML, actually no big problem, with the help of various development tools and open source libraries, efficiency is not low, but the biggest problem with XML is verbose, otherwise it would not be replaced by JSON. Of course, we could use XML for the layout and Kotlin for the code as shown in KotlinHello, and there’s nothing wrong with that, but the great thing about Kotlin is brevity, so using Anko you can do the same thing very succinctly with less code, and with less code, you’re more efficient.
How do I use Anko
Rewrite KotlinHello to show you how to use Anko in your project. Open the KotlinHello project from the previous article and add it to dependencies in the build.gradle section of your app:
|
|
Define the anko_version variable at the top:
|
|
Re-sync gradle and when it’s done, it’s ready to use.
Edit HelloActivity. Kt, inside onCreate, delete everything except super.oncreate, and add:
|
|
When it works, it looks like this:
The page is a little ugly, so I’ll show you how to add layout attributes:
|
|
This is what it ended up being:
Post the full code: app/build.gradle:
|
|
HelloActivity.kt:
|
|
Actual combat, lu a program ape old history
A KotlinHello is still too toy, let’s do a slightly more complicated small project to practice. Considering that the biggest change Kotlin brings is to use Anko to write the layout, so let’s make a slightly more complicated layout. Therefore, we can do a program monkey Lao Calendar, its function is relatively simple, mainly is the layout, and does not involve the network. So suitable for beginners practice.
Need to understand
Before you do anything, understand the requirements. We want to masturbate is this version of the programmer old almanac. The principle is very simple, pre-define some events, tools, drinks, location, etc., and then calculate a random index using the current date, take a batch from the pre-defined, and then display it. In fact, for the logical part of the code, we just copy, don’t care too much. The key is how the layout is implemented with Anko.
To fit a lu
- Create a new package: Calendar
- Create a new Empty Activity in the Calendar: CalendarActivity
|
|
- Skip to CalendarActivity when you click Button in KotlinHello
|
|
- Start the layout of the overall layout is divided into five pieces: head date, appropriate head, appropriate details, bad head, bad details, bottom direction and index. One of these, the header date, can be solved with a TextView. Good and bad, they’re the same thing, you can reuse them, and the good is a list, and the bottom is a list, but because the number and each item is fixed, you can do it with three views.
Conclusion:
- The root layout should be a ScrollView, because if you have a lot of content, or if the screen is too small, there might be some out-of-screen space, so the root layout should be able to slide.
- The middle good/bad, and good or bad specific events, to use a LinearLayout to wrap the two, because the good/bad height is determined by the specific event, and to fill the background color, so the package on a layer of LinearLayout is inevitable.
- So, from top to bottom, a LinearLayout will do
Running effect
Final operation result:
The final code
CalendarActivity, responsible for layout and presentation
|
|
ProgrammerCalendar, here is the business logic
|
|
The full code can be downloaded here.
New features used
From the code, you can see that some of the Kotlin language features are used in addition to the previous article:
Ranges
Can be understood as intervals, used to iterate over some range, as can be seen from the genTodayLuck method in the example. The following is also a simple supplement:
|
|
You can use it if you like.
|
|
The default step size is 1, which can also be customized:
|
|
Extension function
You can add methods to existing classes that are neither inherited nor composed, much like categories in Object-C. This makes it very succinct to perform an operation based on a class, such as the pickRandom method in this example. If the normal implementation passes the list as a parameter, but the Extension function is used as if it were a method provided by the Collection itself. Readability and simplicity are greatly improved.
Companion object
Companion Object is used to declare a companion object within a class. When referring to a companion object member, you omit the name of its class, as in this example. When referencing the ProgrammerCalendar Companion Object EventKeys in CalendarActivity, you can omit:
|
|
The const keyword
Introduced variable on an article, use the var statement, constant val to declare, the const keyword is what the devil? It is used to declare a top-level property of a class (in other words, a non-inner class), which is equivalent to static final in Java:
|
|
The resources
- Building a UI with Kotlin and Anko