Added extensions for TextView compound drawables management (#9)

This commit is contained in:
Ivan Vlasov 2019-02-21 04:04:32 +03:00 committed by GitHub
parent 9672f0eb58
commit da3f1d20cb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 32 additions and 0 deletions

View File

@ -0,0 +1,32 @@
package ru.touchin.extensions
import android.graphics.drawable.Drawable
import android.os.Build
import android.widget.TextView
var TextView.drawableStart: Drawable?
get() = drawables[0]
set(value) = setDrawables(value, drawableTop, drawableEnd, drawableBottom)
var TextView.drawableTop: Drawable?
get() = drawables[1]
set(value) = setDrawables(drawableStart, value, drawableEnd, drawableBottom)
var TextView.drawableEnd: Drawable?
get() = drawables[2]
set(value) = setDrawables(drawableStart, drawableTop, value, drawableBottom)
var TextView.drawableBottom: Drawable?
get() = drawables[3]
set(value) = setDrawables(drawableStart, drawableTop, drawableEnd, value)
private inline val TextView.drawables: Array<Drawable?>
get() = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) compoundDrawablesRelative else compoundDrawables
private fun TextView.setDrawables(start: Drawable?, top: Drawable?, end: Drawable?, bottom: Drawable?) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
setCompoundDrawablesRelativeWithIntrinsicBounds(start, top, end, bottom)
} else {
setCompoundDrawablesWithIntrinsicBounds(start, top, end, bottom)
}
}