Your First Kotlin Android App: An App From Scratch
Part 2: Work with an Android Activity Work with Strings
— Android & Kotlin

Lesson Complete

Play Next Lesson
Next

Work with Strings

This video was last updated on Jul 5 2022

Cover the process of working with the “string” data type which stores a sequence of characters and display the value of the seekbar in the Alert Dialog.

Contributors

It’s time to tackle the next item on our programming to-do list: “Read the value of the slider after the user presses the Hit Me button.” But before you do that, I want to tell you about a very important data type you’ll use in your Android apps: “Strings”.

To create a String in kotlin, you simply surround some text with quotes. Now, behind the scenes, strings are just a sequence of characters. You can imagine them as a bunch of characters hanging on a piece of string, like you see here.

Strings in Kotlin have a cool feature called string templates also known as string interpolation in other programming languages. That’s just a fancy way of saying that you can put placeholder values inside your strings. And these placeholders will be replaced dynamically with code when the app runs.

Imagine you’d like to insert a dynamic value into a string at runtime. For example, maybe you want to say Hello, and then the name of the user of the app. To accomplish this, you simply put a dollar sign in front of the variable inside the string. In this example, if name is set to Joe, at runtime the string will become “Hello, Joe!”.

OR you can use an expression. You just wrap the expression inside curly braces and put a dollar sign in front of the curly braces. The expression is evaluated at runtime and returns the string.

Before we display the value of the seekbar in the dialog, we need to first get the selected value. Let’s do that now.

For this, you’ll set up a listener that monitors when the thumb in the seekbar has been moved.

But before you do that, you need to make a correction. In the last episode, I passed in the string for the hit me button as the dialog’s button text instead of the one we created for it. I’ve corrected that now and you can go ahead and correct yours if you made the same mistake.

Now back to the code. To set up a listener for the seekbar, you need create a variable that will hold the selected value of the seekbar. Add it in the main activity class like so:

private var sliderValue = 0

Next, let’s setup the seekbar’s change listener. Enter the following code below the hitme button’s listener:

binding.seekBar.setOnSeekBarChangeListener(object: SeekBar.OnSeekBarChangeListener {
    
})

This is the structure of the seekbar change listener. Android Studio complains because the argument implements something called an Interface. Here, OnSeekBarChangeListener is the Interface. For now, you can think of an interface like a contract. This contract requires that we implment certain methods. This object must agree to the terms of the contract.

Hover over the object. It tells us that we need to implement members. Click on that item. This opens up a dialog box. You can see that this interface or simply contract expects us to implement three methods. Go ahead and hold down the shift key and select them all then click the OK button.

This adds the three methods you selected. These methods have been overriden from the interface that declared them. So this is the object’s version of these methods. And you can see the override keyword in the method declaration.

I’ll go ahead and clean up the generated code and give the parameters more descriptive names. And by the way the word parameter is used when declaring a method while argument is used when you pass the expected value when calling the method. Not to worry, all these terms would make more sense to you as you progress in the learning path.

Okay i’ll go ahead and do that now.

override fun onProgressChanged(seekBar: SeekBar?, progress: Int, fromUser: Boolean) {
                
}
override fun onStartTrackingTouch(seekBar: SeekBar?) {}

override fun onStopTrackingTouch(seekBar: SeekBar?) {}

The method we’re interested in is the onProgressChanged method. This method is triggered when the progress of the seekbar is changed. For instance when the thumb is moved.

In here, you have access to the seekBar, the progress and the fromUser boolean. A boolean data type is used to store a value that can be either true or false. fromUser will return true if the progress change was initiated by the user for example when the user slides the thumb. If it was changed programmatically then it will return false.

To set the sliderValue to the updated progress value, enter the following code inside the onProgressChanged method:

...
sliderValue = progress

This sets sliderValue variable to the current progress point. Next, you’re going to use this value inside the dialog message of the showResult method:

First, copy the dialogMessage variable. Then convert the current dialog message code into a comment by clicking on that line then press Cmd + /. You can see that double forward slashes are added to that lines and it turns grey. Comments are useful in programming. They can be used to describe some code you write or they can be used to temporarily exclude code that you could add back later.

The kotlin environment would skip those comment when the file is being executed. Using comments is better than deleting the code and trying to remember it when you need it again.

Go ahead and paste the code you copied below the comment and update it to the following:

val dialogMessage = "The slider's value is $sliderValue"

This is an example of kotlin’s string template feature we talked about earlier. This expression will be evaluated to a string where the number will be inserted at that point in the string.

Run your app to see the update. Nice!!! The dialog displays the seekBar’s selected value.

This is cool but we need to use the concept of interpolation in the strings.xml file. Go open up the strings.xml file. Then update the result_dialog_message string to the following:

<string name="result_dialog_message">The slider\'s value is %1$d.</string>

The format that you see here is the placeholder for the number that will be inserted into the string. 1 signifies the position of the item so if we have another item at some other point in the string then it would be denoted with the number 2. The letter d after the dollar sign signifies the type of value passed. d simply means the placeholder would accept a whole number. Strings are denoted with s.

And if you notice, we added a backslash before the single quote. This is done to let the xml processor correctly interpret the single quote as it is a special character.

Head back to the main activity class. Comment the displayMessage variable line. And uncomment the previous displayMessage variable declaration. After that, go ahead and pass in sliderValue as the second argument of the getString() method like so:

val dialogMessage = getString(R.string.result_dialog_message, sliderValue)

Run your app once again.

And you have the slider value correctly displayed using the placeholder from the strings.xml file

Reviews

Comments