image
Dynamic Messages
Start course
Difficulty
Beginner
Duration
3h 39m
Students
38
Ratings
5/5
Description

This course begins by downloading Android studio - make sure you watch the appropriate video depending on whether you're on Mac or Windows. Then, we'll take a tour off the Android Studio interface and see how apps are put together. You'll learn about text views, buttons, and images to build a user interface for our app, and we'll also write some code to make our apps interactive.

Then we'll move onto a practical project in which we make a temperature converter app, which converts temperatures from Fahrenheit to Celsius. You'll be able to follow, building the app, and then running it on your system.

Intended Audience

This course is intended for beginners to Android app development or anyone who wants to master coding in Kotlin.

Prerequisites

Since this is a beginner level course, there are no requirements, but any previous experience with coding would be beneficial.

Transcript

Hello and welcome back. In this video, we'll look at how we can take the email address that the user is entering over here and display it back to the user in our greeting message once they click the button. Right now, our message simply says Hello Mashrur which you can see here and this is hard coded in our code right here. Okay, so to do this, we'll use what's called string interpolation and you may have heard of this as string templates as well. So, let's take a look, I'll pull up my Kotlin playground. And if you didn't use this in the prior video where we looked at strings, you can go to play.kotlinlang.org and it'll pull this up for you. Now you see here I have my main function and I have these two string variables. 

One is greeting which says, "how are you doing today" and another one is myName that has my name in it, Mashrur. Then I have a print line which prints "Hello myName, greeting?" but these are within the string, within the double quotes. So, they are not basically references to my variables but simply part of the text. So, if I click run, you see it prints out "Hello myName, greeting?" and that's not the aim, right? Our aim here is to display the value of the variables here. Whatever the variables are referencing. So, it should say, "Hello Mashrur, how are you doing today?" And how can we do that? We can do that using string interpolation. 

So, I'm going to close out of here. What you do is you turn this string into a string template and I'm simply going to add the dollar sign here behind my variables. So, $myName and then behind greeting, $greeting. Now because of this, what Kotlin will do is instead of directly printing these out, it will treat these two as expressions and the value of the evaluated expressions here simply being variables will print them to the screen. And this again is called a template string so let's run it. Check it out, "Hello Mashrur, how are you doing today?"

Now we can simply change the value of myName or whatever is in the string, the greeting to whatever we want by changing the name here. So, I'll say myName, if I made it John and run, "Hello John, how are you doing today?" Now one additional thing before we move back to Android Studio is we were able to use this notation, this dollar sign and then simply the name of the variable because these are simply variables. If you instead had an expression here instead of just the variable names, then you would have wanted it to be evaluated and you'd have to surround them with curly braces like this, okay? So, let's say instead of myName here, you wanted the result of 10 multiplied by 20. This is how you do it. 

You surround them with curly braces. If I run this, check it out, "Hello 200" so it evaluated that expression, how are you doing today? Great. So, these curly braces are important to keep in mind if you're running into trouble with template strings and you're like, okay, why isn't it working? Just see if it's an evaluated expression or if it's just a variable name. Okay, now with this knowledge, let's add it to our login app. When I go back to Android Studio, here I have the text portion and instead of having this hard coded name, I want to display the email address that the user is entering over here. Email address and that is my emailView. So, to get the text, I can do emailView.text which I'm doing here. So, what I'll do here is instead of Mashrur, simply say $emailView which is my view right here and then I have to get the text for it, .text. But remember this is not a simple variable. 

I have a view and I'm getting the text property of my view over here, which means this is an evaluation of an expression that's happening. So, because of that I have to surround this with curly braces. So, let's run it and see if it works, run up. Okay, I'll pull up the emulator. Great, let's enter in Mashrur and then sign in or register. There we go, "Hello Mashrur". Great, so it's working. I hope you managed to get that to work. Now back here, I could have done this differently, I could have also used a variable instead of doing this here, let's say val emailText and I'll assign this to emailView.text, okay? If I had done this, then I have a variable so I can directly reference it here without the curly braces and done this, emailText. Let's run it and see if it works. 

And one thing I'd like to note is that this message keeps popping up and it's because I'm clicking on this run app and we've done this pretty consistently. Sometimes I've done apply changes and restart activity and I'll explain what the difference is very soon. But for now I'm simply going to do terminate over here. All right, I'll pull up the emulator to make sure it's still working. Mashrur, checkmark sign or register. There we go, hello mashrur. Perfect, so that works as well. All right, and that's it for this video, I hope you enjoyed learning about string templates and using them directly in the app. Thanks for watching and I'll see you in the next video.

 

About the Author

Mashrur is a full-time programming instructor specializing in programming fundamentals, web application development, machine learning and cyber security. He has been a technology professional for over a decade and has degrees in Computer Science and Economics. His niche is building comprehensive career focused technology courses for students entering new, complex, and challenging fields in today's technology space.

Covered Topics