androidandroid-studiokotlinsettext

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

RadioGroup

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

class MainActivity : AppCompatActivity() {


override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)




    val actionButtonhome: ExtendedFloatingActionButton = findViewById(R.id.actionButton_home)

    val bottomNav : BottomNavigationView = findViewById(R.id.bottom_nav_view)
    val radio_group: RadioGroup = findViewById(R.id.radio_group)
    val radio_0: RadioButton = findViewById(R.id.radio_0)
    val radio_1: RadioButton = findViewById(R.id.radio_1)
    val radio_2: RadioButton = findViewById(R.id.radio_2)

    radio_group.setOnCheckedChangeListener {radio_group, optionId -> run {
        when (optionId) {
            R.id.radio_0 -> {
                
                <<-- Problem is this line -->>
                actionButtonhome.text = radio_0.text
                
            }
            R.id.radio_1 -> {
            }
            R.id.radio_2 -> {
            }
            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

error


Solution

  • 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 +.