Home > Enterprise >  TextView background color not changing in android (kotlin) on radio button selection
TextView background color not changing in android (kotlin) on radio button selection

Time:02-01

I have a group of radio buttons to toggle the background color of the textview on an app. Here is the XML for the buttons and the textview

<TextView
            android:id="@ id/calcScreen"
            android:layout_width="wrap_content"
            android:layout_height="500dp"
            android:fontFamily="monospace"
            android:text="0"
            android:textSize="48sp" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="horizontal">

            <RadioGroup
                android:id="@ id/bgColorChangeGRP"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal"
                android:paddingHorizontal="14dp">

                <RadioButton
                    android:id="@ id/_FF0000"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:onClick="onRadioButtonClicked"
                    android:text="Red background"
                    android:textSize="12sp" />

                <RadioButton
                    android:id="@ id/_00FF00"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:onClick="onRadioButtonClicked"
                    android:text="Green background"
                    android:textSize="12sp" />

                <RadioButton
                    android:id="@ id/_0000FF"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:onClick="onRadioButtonClicked"
                    android:text="Blue background"
                    android:textSize="12sp" />
            </RadioGroup>

        </LinearLayout>

As you can see, I have a onClick method added called onRadioButtonClicked, here is the code for that function:

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

    fun onRadioButtonClicked(v: View){
        val textView = findViewById<TextView>(R.id.calcScreen)
        if (v is RadioButton) {
            val checked = v.isChecked
            when(v.getId()){
                R.id._0000FF ->
                    if (checked) {
                        textView.setBackgroundColor(0xFF0000)
                    }
                R.id._00FF00 ->
                    if (checked) {
                        textView.setBackgroundColor(0x00FF00)
                    }
                R.id._0000FF ->
                    if (checked) {
                        textView.setBackgroundColor(0x0000FF)
                    }
            }
        }
    }

}

Whenever I click a button nothing happens. I tried adding a print statement in testing but did not see any output in the text console. I followed the android tutorial but do not seem to get the desired output. I think it has something to do with how the onRadioButtonClicked function is getting the id's but am not 100% sure. Any tips would be appreciated.

CodePudding user response:

if (checked) {
       textView.setBackgroundColor(Color.parseColor("#FFFFFF"))
 }

try to use it as

CodePudding user response:

To clarify why your original solution wasn't working. because you made your button transparent. The first byte is the alpha.

So your colors should have value like: 0xFFFF0000 to make your colors visible. FF at the start is key.

So your code would look something like this:

fun onRadioButtonClicked(v: View){
        val textView = findViewById<TextView>(R.id.calcScreen)
        if (v is RadioButton) {
            val checked = v.isChecked
            when(v.getId()){
                R.id._FF0000 ->
                    if (checked) {
                        textView.setBackgroundColor(0xFFFF0000.toInt())
                    }
                R.id._00FF00 ->
                    if (checked) {
                        textView.setBackgroundColor(0xFF00FF00.toInt())
                    }
                R.id._0000FF ->
                    if (checked) {
                        textView.setBackgroundColor(0xFF0000FF.toInt())
                    }
            }
        }
    }
  •  Tags:  
  • Related