Skip to content

Instantly share code, notes, and snippets.

@AlmightyCZ
Created May 3, 2020 13:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AlmightyCZ/527e11ad07ec67007208c2f5519a296c to your computer and use it in GitHub Desktop.
Save AlmightyCZ/527e11ad07ec67007208c2f5519a296c to your computer and use it in GitHub Desktop.
class TextInputEditText : TextInputEditText {
constructor(context: Context) : super(context)
constructor(context: Context, attrs: AttributeSet?) : super(context, attrs)
constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr)
/**
* Copied from TextInputLayout implementation
*/
private fun getTextInputLayout(): TextInputLayout? {
var parent = parent
while (parent is View) {
if (parent is TextInputLayout) {
return parent
}
parent = parent.getParent()
}
return null
}
private val updateLayoutLabelStateFunction by lazy {
TextInputLayout::class.java.getDeclaredMethod("updateLabelState", Boolean::class.java)
.apply { isAccessible = true }
}
private var _myHint: CharSequence? = null
var myHint: CharSequence?
get() = _myHint
set(value) {
_myHint = value
hint = value
getTextInputLayout()?.let { updateLayoutLabelStateFunction.invoke(it, true) }
}
override fun getText(): Editable? {
val text = super.getText()
if (!text.isNullOrEmpty())
return text
/* We want this expression in TextInputLayout.java to be true if there's a hint set:
* final boolean hasText = editText != null && !TextUtils.isEmpty(editText.getText());
* But for everyone else it should return the real value, so we check the caller.
*/
if (!myHint.isNullOrEmpty() && Thread.currentThread().stackTrace[3].className == TextInputLayout::class.qualifiedName)
return SpannableStringBuilder(myHint)
return text
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment