Created
May 3, 2019 17:44
-
-
Save siyamed/e7276009121d775c0d74ea7d6b01fc25 to your computer and use it in GitHub Desktop.
Show Error in TextInputEditText
This file contains hidden or 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
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 | |
} | |
} |
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
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 usualTextInputEditText