Skip to content

Instantly share code, notes, and snippets.

@siyamed
Created May 3, 2019 17:44
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save siyamed/e7276009121d775c0d74ea7d6b01fc25 to your computer and use it in GitHub Desktop.
Save siyamed/e7276009121d775c0d74ea7d6b01fc25 to your computer and use it in GitHub Desktop.
Show Error in TextInputEditText
import android.content.Context
import android.graphics.Point
import android.graphics.Rect
import android.util.AttributeSet
import android.view.View
import android.view.ViewParent
import com.google.android.material.textfield.TextInputEditText
import com.google.android.material.textfield.TextInputLayout
class MyTextInputEditText : TextInputEditText {
@JvmOverloads
constructor(
context: Context,
attrs: AttributeSet? = null,
defStyleAttr: Int = android.R.attr.editTextStyle
) : super(context, attrs, defStyleAttr) {
}
private val parentRect = Rect()
override fun getFocusedRect(rect: Rect?) {
super.getFocusedRect(rect)
rect?.let {
getMyParent().getFocusedRect(parentRect)
rect.bottom = parentRect.bottom
}
}
override fun getGlobalVisibleRect(rect: Rect?, globalOffset: Point?): Boolean {
val result = super.getGlobalVisibleRect(rect, globalOffset)
rect?.let {
getMyParent().getGlobalVisibleRect(parentRect, globalOffset)
rect.bottom = parentRect.bottom
}
return result
}
override fun requestRectangleOnScreen(rect: Rect?): Boolean {
val result = super.requestRectangleOnScreen(rect)
val parent = getMyParent()
// 10 is a random magic number to define a rectangle height.
parentRect.set(0, parent.height - 10, parent.right, parent.height)
parent.requestRectangleOnScreen(parentRect, true /*immediate*/)
return result;
}
private fun getMyParent(): View {
var myParent: ViewParent? = parent;
while (!(myParent is TextInputLayout) && myParent != null) {
myParent = myParent.parent
}
return if (myParent == null) this else myParent as View
}
}
@AndroidDeveloperLB
Copy link

This is a snippet from here:
https://youtu.be/fpSfCvP36aA?t=1772

Can you please put it in a normal sample on Github, to show how to use it?

@luckyhandler
Copy link

luckyhandler commented Jul 14, 2020

This destroys the rest of my layout. The label doesn't float to the top any more but somewhere inside the TextInputLayout and the MyTextInputEditText is way smaller than when using the usual TextInputEditText

@MisterPotz
Copy link

This code doesn't work for me. Why do you even need to override requestRectangleOnScreen and getFocusedRect if no entity calls that code when keyboard opens? (checked with debugger)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment