Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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

try to use it as

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading