How can I set a button's text to be "this views string" and "this other views string" in Kotlin


Thanks for taking time to help

I have a radio group with 3 radio buttons and a FloatingActionButton in my activity_main.xml


Then, in my MainActivity.kt, I’ve set a setOnCheckedChangeListener to that radio group

class MainActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {

    val actionButtonhome: ExtendedFloatingActionButton = findViewById(

    val bottomNav : BottomNavigationView = findViewById(
    val radio_group: RadioGroup = findViewById(
    val radio_0: RadioButton = findViewById(
    val radio_1: RadioButton = findViewById(
    val radio_2: RadioButton = findViewById(

    radio_group.setOnCheckedChangeListener {radio_group, optionId -> run {
        when (optionId) {
   -> {
                <<-- Problem is this line -->>
                actionButtonhome.text = radio_0.text
   -> {
   -> {
            else -> {

What I would like, is to have the button’s Text as the radio button text + "this string"

But when I try actionButtonhome.text = radio_0.text + "_" + "this string"
I get a huge error and the + symbols are highlighted red



Replace radio_0.text with radio_0.text.toString(). The problem is that .text returns a CharSequence instead of a String and you can’t concatenate CharSequences with +.

Answered By – Tenfour04

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published