Taking Advantage of Kotlin - Part II

Taking Advantage of Kotlin - Part II


Let's get set up. I'm sure you have the all requirements that I have said earlier in last post. Let's try to convert a Java - coded app converting into Kotlin.

  1. First, You have to download the project, The Address Book.
  2. Unpack the Zip.
  3. MyAddressBook-starter contains the starter app
  4. Steps 1 and 2 are the introduction and getting started steps, so there are no folders for these ones.
  5. MyAddressBook-stepN folders contain the app in the finished state after step N.
  6. The final app can be found in the MyAddressBook-final folder.

App Overview

MyAddressBook is a simplified address book app that displays a list of contacts in a RecyclerView. You'll be converting all of the Java code into Kotlin, so take a moment to familiarize yourself with the existing code in the MyAddressBook-starter app.
  1. Open MyAddressBook-starter in Android Studio.
  2. Run it.

Contact.java


The Contact class is a simple Java class that contains data. It is used to model a single contact entry and contains three fields: first name, last name and email.

ContactsActivity.java


The ContactsActivity shows a RecyclerView that displays an ArrayList of Contact objects. You can add data in two ways:
  1. Pressing the Floating Action Button will open up a New Contact dialog, allowing you to enter contact information.
  2. You can generate a list of 20 mock contacts quickly by selecting the Generate option in the options menu, which will use an included JSON file to generate Contact objects.
Once you have some contacts, you can swipe on a row to delete that contact, or else clear the whole list by selecting Clear in the options menu.
You can also tap on a row to edit that contact entry. In the editing dialog the first and last name fields are disabled, since for this tutorial app, these fields are immutable (only getters are provided in the Contact class), but the email field is modifiable.
The New Contact dialog performs validation in the following ways:
  • The first and last name fields must not be empty.
  • The email field must contain an email address using a valid format.
  • Attempting to save an invalid contact will show an error in a Toast message.

Configure Kotlin

Before you can use the Kotlin tools and methods, you must configure Kotlin in your project.
  1. In Android Studio, select Tools > Kotlin > Configure Kotlin in Project. If a window titled Choose Configuratorappears, select Android with Gradle, make sure All modules is selected, and click OK.
Android Studio will add the required dependencies in both the app level and the project level build.gradle files.
  1. You will be prompted to sync the project with Gradle as the build.gradle files have changed. Once the sync is complete, move to the next step and write some Kotlin code.


Stay tuned - part III follows


Comments

Popular posts from this blog

Unity Guide for newbies - Part II