parent
aa07613554
commit
5c0dac7e62
@ -0,0 +1,60 @@ |
|||||||
|
package net.pokeranalytics.android.ui.view |
||||||
|
|
||||||
|
import android.content.Context |
||||||
|
import android.util.AttributeSet |
||||||
|
import androidx.appcompat.widget.AppCompatTextView |
||||||
|
import androidx.core.content.ContextCompat |
||||||
|
import net.pokeranalytics.android.R |
||||||
|
import net.pokeranalytics.android.util.toCurrency |
||||||
|
|
||||||
|
/** |
||||||
|
* Extended TextView to display amount in the right color / currency |
||||||
|
* Work in progress |
||||||
|
*/ |
||||||
|
class DynamicValueTextView: AppCompatTextView { |
||||||
|
|
||||||
|
var isCurrency: Boolean = false |
||||||
|
var amount: Double = 0.0 |
||||||
|
set(value) { |
||||||
|
field = value |
||||||
|
updateUI() |
||||||
|
} |
||||||
|
|
||||||
|
constructor(context: Context?) : super(context) { |
||||||
|
init(null) |
||||||
|
} |
||||||
|
|
||||||
|
constructor(context: Context?, attrs: AttributeSet?) : super(context, attrs) { |
||||||
|
init(attrs) |
||||||
|
} |
||||||
|
|
||||||
|
constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr) { |
||||||
|
init(attrs) |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Init class |
||||||
|
*/ |
||||||
|
private fun init(attrs: AttributeSet?) { |
||||||
|
context.theme.obtainStyledAttributes( attrs, R.styleable.DynamicValueTextView, 0, 0).apply { |
||||||
|
try { |
||||||
|
isCurrency = getBoolean(R.styleable.DynamicValueTextView_isCurrency, false) |
||||||
|
amount = getFloat(R.styleable.DynamicValueTextView_amount, 0f).toDouble() |
||||||
|
updateUI() |
||||||
|
} finally { |
||||||
|
recycle() |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Update the UI to manage currency format & color management |
||||||
|
*/ |
||||||
|
private fun updateUI() { |
||||||
|
if (isCurrency) { |
||||||
|
setTextColor(ContextCompat.getColor(context, if (amount >= 0) R.color.green else R.color.red)) |
||||||
|
text = amount.toCurrency() |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
} |
||||||
@ -1,6 +1,6 @@ |
|||||||
<?xml version="1.0" encoding="utf-8"?> |
<?xml version="1.0" encoding="utf-8"?> |
||||||
<resources> |
<resources> |
||||||
<declare-styleable name="PokerAnalyticsTextView"> |
<declare-styleable name="DynamicValueTextView"> |
||||||
<attr name="isCurrency" format="boolean" /> |
<attr name="isCurrency" format="boolean" /> |
||||||
<attr name="amount" format="float" /> |
<attr name="amount" format="float" /> |
||||||
</declare-styleable> |
</declare-styleable> |
||||||
|
|||||||
Loading…
Reference in new issue