Joel Broström
Joel Broström

Reputation: 4090

Kotlin: format string

I have a Recycle view that i want to iterate over and add a bullet point in front of each input String.

I have the following:

value.strings.xml

<string name="skill">• %1$s</string>

ProfileListAdapter:

fun bindSkills(skill: String) {

            itemView.recycleSkillItem.text = String.format(Locale(R.string.skill.toString()), ${R.string.skill}, skill)
    }

This prints the same int over and over again without the bullet point.

what is the best practice to format strings in kotlin?

Upvotes: 3

Views: 6114

Answers (1)

Joshua
Joshua

Reputation: 6241

You should learn how Android works first. R.string.skill returns the resource id of the string which is an Int To get the String, from resource, you have to use

context.getString(resId)

It also supports string formatting.

context.getString(R.string.skill, skill)

context can be anything that extends Context, i.e Activity, Fragment.

To format String in Kotlin, you use string interpolation.

val world = "World"
val helloWorld = "Hello $world"  // Hello World

Upvotes: 9

Related Questions