Reputation: 75
I want to make an if statement in which I will compare the color of my layout background color,with kotlin something like this:
val viewPaint = (background.getBackground() as PaintDrawable).paint
val colorARGB = viewPaint.color
if(colorARGB == Color.GREEN){
btn_touch.setOnClickListener {
counter += 1
textCounter.text = counter.toString()
}
}
It's not working how can i compare background color with kotlin ?
Upvotes: 1
Views: 757
Reputation: 76458
I answered your other question, so I have a bit more context :-) Instead of comparing the background colors directly you should store your own State of your app, so that you can control what it is up to, and let the background react accordingly.
enum class State {
ON,
OFF
}
From your GitHub repo, change your loop to update the state:
when (Random.nextBoolean()) {
true -> state = State.ON
false -> state = State.OFF
}
Then use that state to update your UI:
private fun updateUI() {
when (state) {
State.ON -> {
background.setBackgroundColor(Color.GREEN)
}
State.OFF -> {
background.setBackgroundColor(Color.RED)
}
}
}
And so you can then use the state again to react to a button click:
btn_touch.setOnClickListener {
when (state) {
State.ON -> reactOnClickWhenOn()
State.OFF -> reactOnClickWhenOff()
}
}
private fun reactOnClickWhenOn() {
counter += 1
}
private fun reactOnClickWhenOff() {
Toast.makeText(this, "Nope!", Toast.LENGTH_SHORT).show()
}
The full example:
import android.graphics.Color
import android.os.Bundle
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import kotlinx.android.synthetic.main.foo.*
import kotlinx.coroutines.*
import java.util.concurrent.TimeUnit
import kotlin.random.Random
class FooActivity : AppCompatActivity() {
private var state = State.OFF
private var counter: Int = 0
private var running = false
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.foo)
val loop = true
GlobalScope.launch(Dispatchers.IO) {
while (loop) {
while (running) {
delay(TimeUnit.SECONDS.toMillis(Random.nextLong(5)))
when (Random.nextBoolean()) {
true -> state = State.ON
false -> state = State.OFF
}
withContext(Dispatchers.Main) {
updateUI()
}
}
}
}
btn_touch.setOnClickListener {
when (state) {
State.ON -> reactOnClickWhenOn()
State.OFF -> reactOnClickWhenOff()
}
}
}
private fun updateUI() {
when (state) {
State.ON -> {
background.setBackgroundColor(Color.GREEN)
}
State.OFF -> {
background.setBackgroundColor(Color.RED)
}
}
}
private fun reactOnClickWhenOn() {
counter += 1
}
private fun reactOnClickWhenOff() {
Toast.makeText(this, "Nope!", Toast.LENGTH_SHORT).show()
}
override fun onResume() {
super.onResume()
running = true
}
override fun onPause() {
running = false
super.onPause()
}
enum class State {
ON,
OFF
}
}
Upvotes: 1
Reputation: 20616
I do not know what is this background
of your code but definitely, javax.swing
is not supported by Android.
You want to use android.view
to use getBackground()
instead.
Remove this import, and if it asks for import something, add the Android
one, but it should not ask for anything, since it's from android.view
.
background is layout id
Then you should use the synthetic
from kotlin
import kotlinx.android.synthetic.main.your_layout.*
Upvotes: 0