Your First Kotlin Android App: An App From Scratch
Part 2: Work with an Android Activity Learn About Instances, Data & Functions
— Android & Kotlin

Lesson Complete

Play Next Lesson
Next

Learn About Instances, Data & Functions

This video was last updated on Jul 5 2022

Get introduced to some basic object-oriented programming concepts to better understand the Kotlin code you’ll be working with in the Android Activity.

Contributors

At this point, you’ve learned how to build a basic user interface using the design view and xml. You’ve learned that user interfaces in Android are built using views and you explored some of them.

You’ve seen the power of ConstraintLayout and how to set the properties of Views using the attributes panel. Also, you saw how to access views inside an activity.

Good Job!!!

The kotlin code you add to the Android Activity will be responsible for adding functionaly to our app. But before we go further, I know some of the code you added in the last episode might not make sense to you yet. You need some understanding of the Kotlin programming language to understand more about what you did.

Kotlin is an object-oriented language.

If you’re already familiar with Object Oriented Programming and feel comfortable working with one - at least from a high-level point of view - you might feel like skipping this but I’ll personally encourage you to watch this episode.

Who knows, maybe you might pick up a thing or two or it could help as a refresher for the knowledge you have.

Now since kotlin is an object-oriented language, it means the code you’ll be writing will contain objects of some kind. The MainActivity class will be responsible for adding functionality to your app.

This means that it will contain references to the textviews and button instances. Well those references to button and textviews are all objects or more specifically, instances of classes.

When you write an android app, you’ll be using prebuilt classes such as button or textview. Or you can create classes of your own such as when you create a new activity based on an existing activity such as AppCompatActivity.

But what is a class?

Well, programmers like to group related data and functionality in small pieces. For example, lets say you want to display a list of dogs in your app. To do this, you first need to define a template for what you need to know about each dog for example, its name and its breed.

Once you have that template, you can create multiple instances of that template.

You can think of instances as just filled-in versions of those templates. For example, you create an instance for Ebuka the poodle or Rooney the Maltese.

This template or blueprint is called a class. To define a class you use the keyword class

You can think of your android app as a bunch of instances of classes that communicate with each other. So far you’ve already instantiated classes and made use of instances.

For instantiating templates, think back to how you created an instance of ActivityMainBinding. ActivityMainBinding class was generated for you when you enabled viewBinding. And the binding variable you create is an instance of the ActivityMainBinding class.

The textView you also accessed from the binding variable is also an instance of the TextView class. Instances of a class are also know as objects. So a dog instance can be called a dog object. And a textView instance can also be called a textview object.

As you can see android provides you with many templates and you can also create your own too.

A class or an instance of a class can have data and functionality.

Take the hit me button in your app for example. It has some data like the text to display, its position on the screen, its width and height and so on.

The data elements in a class are known as its properties. You’ve created a property in the MainActivity class: the binding data which you declared. And this binding data has access to the views in our layout file.

Also, a class has some functionalities. Take a button class for example. It has the ability to recognize when a user taps on it, Display an animated ripple effect when tapped, and also Trigger an action in response to the tap.

These functionalities are commonly known as functions, no pun intended. And also known as methods in some other programming languages.

So i’ll be using the two terms interchangeably. You haven’t created any functions of your own yet but you’ll be doing so soon.

If you have an instance of a class, you call a method on it by appending a dot and the functionName, and enclosing any arguments you need to send to the function in parentheses.

On the other hand, you’ve worked on code inside a function. You added some code inside the onCreate function of the MainActivity class. And you can tell it’s a function because of the fun keyword. The code you’ve written there is always executed whenever the activity has been created. And that’s why you could get the target textview and change its text.

Not to worry you’ll be creating different functions as you progress in this course.

The concept of functions may still feel a little bit weird to you, so let’t take a look at an example.

You (or at least an instance named “You”) wants to throw a party. You clean the room, and put on some music. But you forgot to buy some cookies!

Fortunately, you’ve invited the instance named Steve who happens to live next door to a convenience store. So you call a function on Steve asking him to bring you some cookies.

The processor now switches it’s attention to the object Steve and executes the commands from his buyCookies() method, one by one, from top to bottom.

When the buyCookies() method is done, the computer returns to your throwParty() function and continues where you left off, so you and your friends can eat the cookies that Steve brought back with him.

The Steve instance also has data. Before he goes to the store he has money.

At the store he exchanges this money data for other, much more important, data: “cookies!” After making that transaction, he brings the cookies back over to the party, and you add them to your drink and snack table.

If he eats the cookies along the way, well - your program has a problem popularly known as a bug.

Another important concept when working with methods is that you’ll often need to change their default behavior by doing what is called an override.

This is generally the case when your class inherits from a parent class. That is, deriving the data and methods in your class from the parent class.

For example, the Steve instance inherited a few methods from his parent object.

You’ve already seen an example of overriding. The onCreate method inside the Activity is an overriden method from MainActivity's parent class.

It must be overriden because all activties in android are required to set a window to display views whenever an activity is created.

Wow!!! That was a lot of information in just one episode. Let’s review some key points.

The most important thing to remember from this lecture is that instances of classes contain two things. First, data, like the money to buy some cookies, and second, functions - like the steps involved in buying some cookies.

Instances can look at each other’s data - well, to some extent at least. After all, Steve may not approve of it if you peek inside his wallet.

Asking instances to perform their functions is how you get your app to do things.

Don’t worry if you don’t get everything right away or if you have trouble understanding some of the syntax.

There’s a lot concepts here that may be brand new to you.

We’ll review these concepts again and again throughout this course and the rest of the courses in our learning path until they feel like second nature.

Again: it’s all about learning via repetition.

Reviews

Comments