Using Kotlin in Android Studio Create 4 big buttons that fill the screen Change the background colour of all the other buttons except the button you clicked.  Colour changes between two colours. Initialize the buttons with one colour.   I have the below code. I would like to have it so that if you then pressed another button it changed the other buttons again and so on. I assume I should use an if statement where depending on the color of the active button the others changed to the opposite, but I have tried that and I can not get it to work as I can not find how to get it to pick up the background colour.  MainActivity.kt import android.graphics.Color import androidx.appcompat.app.AppCompatActivity import android.os.Bundle import android.widget.Button class MainActivity : AppCompatActivity() {     lateinit var b1: Button     lateinit var b2: Button     lateinit var b3: Button     lateinit var b4: Button     override fun onCreate(savedInstanceState: Bundle?) {         super.onCreate(savedInstanceState)         setContentView(R.layout.activity_main)         b1 = findViewById(R.id.button1); btn1.setBackgroundColor(getColor(R.color.black))         b2 = findViewById(R.id.button2); btn2.setBackgroundColor(getColor(R.color.black))         b3 = findViewById(R.id.button3); btn3.setBackgroundColor(getColor(R.color.black))         b4 = findViewById(R.id.button4); btn4.setBackgroundColor(getColor(R.color.black))                 b1.setOnClickListener {             b2.setBackgroundColor(getColor(R.color.teal_200))             b3.setBackgroundColor(getColor(R.color.teal_200))             b4.setBackgroundColor(getColor(R.color.teal_200))         }         b2.setOnClickListener{             btn1.setBackgroundColor(getColor(R.color.teal_200))             btn3.setBackgroundColor(getColor(R.color.teal_200))             btn4.setBackgroundColor(getColor(R.color.teal_200))         }         b3.setOnClickListener{             b1.setBackgroundColor(getColor(R.color.teal_200))             b2.setBackgroundColor(getColor(R.color.teal_200))             b4.setBackgroundColor(getColor(R.color.teal_200)         }         b4.setOnClickListener{             b1.setBackgroundColor(getColor(R.color.teal_200))             b2.setBackgroundColor(getColor(R.color.teal_200))             b3.setBackgroundColor(getColor(R.color.teal_200))         }     } } activity_main.xml

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question
100%

Using Kotlin in Android Studio

Create 4 big buttons that fill the screen

Change the background colour of all the other buttons except the button you clicked. 

Colour changes between two colours.

Initialize the buttons with one colour.

 

I have the below code. I would like to have it so that if you then pressed another button it changed the other buttons again and so on. I assume I should use an if statement where depending on the color of the active button the others changed to the opposite, but I have tried that and I can not get it to work as I can not find how to get it to pick up the background colour. 

MainActivity.kt

import android.graphics.Color
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button

class MainActivity : AppCompatActivity() {

    lateinit var b1: Button
    lateinit var b2: Button
    lateinit var b3: Button
    lateinit var b4: Button

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

        b1 = findViewById(R.id.button1); btn1.setBackgroundColor(getColor(R.color.black))
        b2 = findViewById(R.id.button2); btn2.setBackgroundColor(getColor(R.color.black))
        b3 = findViewById(R.id.button3); btn3.setBackgroundColor(getColor(R.color.black))
        b4 = findViewById(R.id.button4); btn4.setBackgroundColor(getColor(R.color.black))
       
        b1.setOnClickListener {

           
b2.setBackgroundColor(getColor(R.color.teal_200))
            b3.setBackgroundColor(getColor(R.color.teal_200))
            b4.setBackgroundColor(getColor(R.color.teal_200))
        }

       
b2.setOnClickListener{

           
btn1.setBackgroundColor(getColor(R.color.teal_200))
            btn3.setBackgroundColor(getColor(R.color.teal_200))
            btn4.setBackgroundColor(getColor(R.color.teal_200))
        }
       
b3.setOnClickListener{

           
b1.setBackgroundColor(getColor(R.color.teal_200))
            b2.setBackgroundColor(getColor(R.color.teal_200))
            b4.setBackgroundColor(getColor(R.color.teal_200)
        }

       
b4.setOnClickListener{

           
b1.setBackgroundColor(getColor(R.color.teal_200))
            b2.setBackgroundColor(getColor(R.color.teal_200))
            b3.setBackgroundColor(getColor(R.color.teal_200))
        }
   
}
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/button1"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_margin="3dp"
        android:textColor="@color/white"
        android:text="Button1"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/button2"
        app:layout_constraintBottom_toTopOf="@id/button3"/>

    <Button
        android:id="@+id/button2"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_margin="3dp"
        android:textColor="@color/white"
        android:text="Button2"
        app:layout_constraintStart_toEndOf="@+id/button1"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toTopOf="@+id/button4"/>

    <Button
        android:id="@+id/button3"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_margin="3dp"
        android:textColor="@color/white"
        android:text="Button3"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/button4"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/button1"/>

    <Button
        android:id="@+id/button4"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:textColor="@color/white"
        android:text="Button4"
        android:layout_margin="3dp"
        app:layout_constraintTop_toBottomOf="@id/button2"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@+id/button3" />

</androidx.constraintlayout.widget.ConstraintLayout>

Expert Solution
steps

Step by step

Solved in 2 steps

Blurred answer
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY