Notes of a Web Developer Learning Kotlin and Android Development - part 2

Eevis - Aug 18 '22 - - Dev Community

This blog post is the second in a series of posts about me working through the Android Basics on Kotlin-course. You can find the first one here: Notes of a Web Developer Learning Kotlin and Android Development - part 1

The second unit of the course, "Android Basics in Kotlin," is about Layouts. It also goes through some Kotlin-concepts, such as lists and with-syntax. From the Android side, the most significant theme is layouts, particularly adapters.

Kotlin: Lists and Other Things

Lists

There are different list types in Kotlin. Lists can be either mutable or immutable - you need to decide which one when defining the list. That's different from JavaScript: Even though you define them as constant in JS, you can still add and remove things. In Kotlin, you need to have a mutable list to do that.

Lists are defined with the following syntaxes:

val immutableList = listOf("list", "of", "words")
val mutableList = mutableListOf("other", "words")
Enter fullscreen mode Exit fullscreen mode

Printing these would result in

[list, of, words]
Enter fullscreen mode Exit fullscreen mode

and

[other, words]
Enter fullscreen mode Exit fullscreen mode

You can get the first item from a list with method first() and the last item with method last(). Sorting a list happens with the sorted()-method, and you can reverse a list with reversed().

Mutable lists have a set of additional methods to mutate the list; here are some examples:

add()
Add an item to the list.
addAll()
Add a collection to the list. More about other collection types in the next part.
remove()
Remove an item from the list.
clear()
Clear the list.
isEmpty()
Check if the list is empty.

Again, here's plenty more than what you'll have with JavaScript. I mean, you can do all of these with JS, but you need to write more code for that, and it's not as verbose. Let's take a look at checking if a list is empty. With Kotlin, it would be:

// Kotlin

val list = listOf(...)
val isListEmpty = list.isEmpty()
Enter fullscreen mode Exit fullscreen mode

And with JavaScript:

// JavaScript

const list = [...]
const isListEmpty = list.length === 0
// or
const isListEmptyShorter = !list.length
Enter fullscreen mode Exit fullscreen mode

A final thing about the lists in this post is looping through them. The material mentions two methods - while and for-loops.

while-loops go on, as long its condition is satisfied. It first checks the condition and executes the body if it's true.

while (x < 10) {
    // Do something
    ++x
}
Enter fullscreen mode Exit fullscreen mode

For-loops iterates through the collection provided. So, for example, looping through the range of numbers could look like this:

for (i in 1..6) {
   // Do something with the current number
}
Enter fullscreen mode Exit fullscreen mode

with

with-syntax is nice when working with, for example, a specific class instance, and there's a need to access more than one of its properties and functions.

So, for example:

with(cat) {
    println("Cat's name is ${name}")
    println("It often sleeps in ${favoritePlace}")
    meow()
}
Enter fullscreen mode Exit fullscreen mode

I could access these properties with cat.name or cat.meow(), but I need to write less code this way.

Android: Let's Talk About Adapters

Adapters are the most significant learning in this unit - or refresher, as I recall some distant things about them. They work as, you guessed, adapters between views and underlying data.

The adapter is a design pattern. It adapts the data and transforms it into something the client can use. In the course, the example is about RecyclerView, and how the data is adapted to it.

When introducing the adapters, the app being built is a list of affirmations. RecyclerView is suitable for this project as it's a good component for creating dynamic lists efficiently.

The course explains how an adapter works with RecyclerView in the app with the following words:

When you run the app, RecyclerView uses the adapter to figure out how to display your data on screen. RecyclerView asks the adapter to create a new list item view for the first data item in your list. Once it has the view, it asks the adapter to provide the data to draw the item. This process repeats until the RecyclerView doesn't need any more views to fill the screen. If only 3 list item views fit on the screen at once, the RecyclerView only asks the adapter to prepare those 3 list item views (instead of all 10 list item views).

In the app, the adapter has multiple parts. First, a layout is created for an individual list item. The next step is to create an ItemAdapter-class, which takes in a list of affirmations, and context-object instance.

After that, an ItemViewHolder is created inside the ItemAdapter. It represents a single list item view in the RecyclerView and can be reused. That view holder is the one that handles updating and showing data.

The adapter needs to implement three methods: onCreateViewHolder, getItemCount and onBindViewHolder.

The layout manager calls onCreateViewHolder to create new view holders for the RecyclerView, if there are no existing view holders that it could reuse.

getItemCount is for, as the name suggests, getting the item count for the list data. The layout manager calls onBindViewHolder to replace the contents of a list item view.

There's one more step: Once the adapter is created, RecyclerView needs to know about it. In the course project, MainActivity is the place to do that. To be more specific, in the onCreate-method of that activity.

I've had a hard time wrapping my head around the pattern. However, I think it's partly because it's a new, complex thing, and to be honest, it's been a while since the last last time I did object-oriented programming.

So I need to keep practicing - this concept among all the others. Luckily, from what I've understood, this is something I'll need many times when learning more about Android development.

Wrapping Up

There were many things to learn in unit 2 of the Android Basics in Kotlin-course. I know I will return to these topics in the following units - and definitely when I start creating something of my own.

The next unit is about navigation, and that's an interesting one. I've already started it, and there's so much to know! I'm so excited about these new concepts I'm learning. As there is so much material and new things in the Navigation unit, I might split it into two posts. We'll see.

Have you been learning Kotlin or Android development? Have any tips or thoughts?

Links

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .