Useful android function extensions to help workflow.
import android.content.res.Resources
import android.graphics.PorterDuff
import android.graphics.PorterDuffColorFilter
import android.graphics.drawable.Drawable
import android.support.design.widget.FloatingActionButton
import android.support.design.widget.TextInputLayout
import android.support.v4.content.res.ResourcesCompat
import android.text.method.KeyListener
import android.util.Log
import android.util.TypedValue
import android.view.View
import android.view.ViewGroup
import android.view.animation.Animation
import android.view.animation.AnimationUtils
import android.widget.EditText
/**
* Created by lauri on 11/01/2018.
*/
/* ---- VIEWS ---- */
// -- Animations--
fun View.startAnimation(animation: Int) {
this.startAnimation(AnimationUtils.loadAnimation(context, animation))
}
private val atAnimationEndSetVisibleMap: HashMap<Int, Boolean> = hashMapOf()
fun View.animateVisibility(animation: Int, setVisible: Boolean) {
if (((this.visibility == View.GONE || this.visibility == View.INVISIBLE) && !setVisible) || (this.visibility == View.VISIBLE && setVisible && this.animation == null)) return
val anim = AnimationUtils.loadAnimation(context, animation)
if (setVisible) {
this.visibility = View.VISIBLE
}
anim.setAnimationListener(object: Animation.AnimationListener {
override fun onAnimationRepeat(p0: Animation?) {}
override fun onAnimationEnd(p0: Animation?) {
atAnimationEndSetVisibleMap.remove(this@animateVisibility.id)
if (!setVisible) this@animateVisibility.visibility = View.GONE
}
override fun onAnimationStart(p0: Animation?) {}
})
if (this.animation != null) {
if (atAnimationEndSetVisibleMap[this.id] != null) {
if (atAnimationEndSetVisibleMap[this.id] != setVisible) {
this.animation.setAnimationListener(null)
this.animation.cancel()
atAnimationEndSetVisibleMap.put(this@animateVisibility.id, setVisible)
this.startAnimation(anim)
}
} else {
atAnimationEndSetVisibleMap.remove(this@animateVisibility.id)
this.animation.setAnimationListener(null)
this.animation.cancel()
}
} else {
atAnimationEndSetVisibleMap.put(this@animateVisibility.id, setVisible)
this.startAnimation(anim)
}
}
// -- Color --
fun FloatingActionButton.setVectorColor(theme: Resources.Theme, color: Int, isColorAttribute: Boolean = false) {
if (!isColorAttribute) {
this.colorFilter = PorterDuffColorFilter(ResourcesCompat.getColor(resources, color, theme), PorterDuff.Mode.SRC_ATOP)
} else {
val typedVal = TypedValue()
theme.resolveAttribute(color, typedVal, true)
this.colorFilter = PorterDuffColorFilter(typedVal.data, PorterDuff.Mode.SRC_ATOP)
}
}
// -- EditText --
val disabledEditTexts: HashMap<EditText, Pair<KeyListener, Drawable>> = hashMapOf()
fun EditText.disableEdit() {
if (disabledEditTexts.contains(this)) disabledEditTexts.remove(this)
disabledEditTexts[this] = Pair(keyListener, background)
if ((parent as ViewGroup).parent is TextInputLayout) ((parent as ViewGroup).parent as TextInputLayout).isHintAnimationEnabled = false
isFocusable = false
isClickable = false
isCursorVisible = false
this.keyListener = null
background = null
}
fun EditText.enableEdit() {
if (!disabledEditTexts.contains(this)) return
if ((parent as ViewGroup).parent is TextInputLayout) ((parent as ViewGroup).parent as TextInputLayout).isHintAnimationEnabled = true
isFocusableInTouchMode = true
isFocusable = true
isClickable = true
isCursorVisible = true
keyListener = disabledEditTexts[this]!!.first
background = disabledEditTexts[this]!!.second
disabledEditTexts.remove(this)
}