Button color: gray default, green pressed via setBackgroundColor
This commit is contained in:
parent
5a1f1da329
commit
5fb67fdcf0
1 changed files with 13 additions and 23 deletions
|
|
@ -3,7 +3,7 @@ package com.eddy.atomrelay
|
|||
|
||||
import android.content.Intent
|
||||
import android.content.pm.PackageManager
|
||||
import android.graphics.drawable.GradientDrawable
|
||||
import android.graphics.Color
|
||||
import android.os.Bundle
|
||||
import android.os.Handler
|
||||
import android.os.Looper
|
||||
|
|
@ -147,25 +147,20 @@ class MainActivity : AppCompatActivity(), BleCallbacks {
|
|||
private fun setupRelayButton() {
|
||||
relay1Button.setOnClickListener(null)
|
||||
loadButtonLabel(relay1Button, PrefsHelper.PREF_LABEL1, R.string.button_hold)
|
||||
|
||||
val defaultBg = GradientDrawable().apply {
|
||||
setColor(ContextCompat.getColor(this@MainActivity, R.color.colorPrimary))
|
||||
cornerRadius = 8f * resources.displayMetrics.density
|
||||
}
|
||||
val pressedBg = GradientDrawable().apply {
|
||||
setColor(ContextCompat.getColor(this@MainActivity, R.color.colorAccent))
|
||||
cornerRadius = 8f * resources.displayMetrics.density
|
||||
}
|
||||
val a = Color.rgb(0xE0, 0xE0, 0xE0)
|
||||
val b = ContextCompat.getColor(this, R.color.colorAccent)
|
||||
relay1Button.backgroundTintList = null
|
||||
relay1Button.setBackgroundColor(a)
|
||||
relay1Button.setOnTouchListener { _, event ->
|
||||
when (event.action) {
|
||||
MotionEvent.ACTION_DOWN -> {
|
||||
manager.sendRelayCommand(0x01.toByte())
|
||||
relay1Button.background = pressedBg
|
||||
relay1Button.setBackgroundColor(b)
|
||||
true
|
||||
}
|
||||
MotionEvent.ACTION_UP, MotionEvent.ACTION_CANCEL -> {
|
||||
manager.sendRelayCommand(0x00.toByte())
|
||||
relay1Button.background = defaultBg
|
||||
relay1Button.setBackgroundColor(a)
|
||||
true
|
||||
}
|
||||
else -> false
|
||||
|
|
@ -176,25 +171,20 @@ class MainActivity : AppCompatActivity(), BleCallbacks {
|
|||
private fun setupRelay2Button() {
|
||||
relay2Button.setOnClickListener(null)
|
||||
loadButtonLabel(relay2Button, PrefsHelper.PREF_LABEL2, R.string.button_hold)
|
||||
|
||||
val defaultBg = GradientDrawable().apply {
|
||||
setColor(ContextCompat.getColor(this@MainActivity, R.color.colorPrimary))
|
||||
cornerRadius = 8f * resources.displayMetrics.density
|
||||
}
|
||||
val pressedBg = GradientDrawable().apply {
|
||||
setColor(ContextCompat.getColor(this@MainActivity, R.color.colorAccent))
|
||||
cornerRadius = 8f * resources.displayMetrics.density
|
||||
}
|
||||
val a = Color.rgb(0xE0, 0xE0, 0xE0)
|
||||
val b = ContextCompat.getColor(this, R.color.colorAccent)
|
||||
relay2Button.backgroundTintList = null
|
||||
relay2Button.setBackgroundColor(a)
|
||||
relay2Button.setOnTouchListener { _, event ->
|
||||
when (event.action) {
|
||||
MotionEvent.ACTION_DOWN -> {
|
||||
manager.sendRelayCommand(0x02.toByte())
|
||||
relay2Button.background = pressedBg
|
||||
relay2Button.setBackgroundColor(b)
|
||||
true
|
||||
}
|
||||
MotionEvent.ACTION_UP, MotionEvent.ACTION_CANCEL -> {
|
||||
manager.sendRelayCommand(0x03.toByte())
|
||||
relay2Button.background = defaultBg
|
||||
relay2Button.setBackgroundColor(a)
|
||||
true
|
||||
}
|
||||
else -> false
|
||||
|
|
|
|||
Loading…
Reference in a new issue