-
-
Save AlmightyCZ/527e11ad07ec67007208c2f5519a296c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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