Skip to content

Instantly share code, notes, and snippets.

@akexorcist
Last active October 8, 2021 00:14
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 akexorcist/33c20f85bb23d9884b380f7b02d32e7f to your computer and use it in GitHub Desktop.
Save akexorcist/33c20f85bb23d9884b380f7b02d32e7f to your computer and use it in GitHub Desktop.
Custom Android UI for prevent the view behind on-screen keyboard when edit text is focused
import android.content.Context
import android.graphics.Point
import android.graphics.Rect
import android.util.AttributeSet
import android.view.View
import androidx.appcompat.widget.AppCompatEditText
class GroupFocusableEditText : AppCompatEditText {
private var parentRect = Rect()
constructor(context: Context) : super(context)
constructor(context: Context, attrs: AttributeSet?) : super(context, attrs)
constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(
context,
attrs,
defStyleAttr
)
override fun getFocusedRect(r: Rect?) {
super.getFocusedRect(r)
getFocusableGroupLayout()?.let { view ->
view.getFocusedRect(parentRect)
r?.bottom = parentRect.bottom
}
}
override fun getGlobalVisibleRect(r: Rect?, globalOffset: Point?): Boolean {
val result = super.getGlobalVisibleRect(r, globalOffset)
getFocusableGroupLayout()?.let { view ->
view.getGlobalVisibleRect(parentRect, globalOffset)
r?.bottom = parentRect.bottom
}
return result
}
override fun requestRectangleOnScreen(rectangle: Rect?): Boolean {
val result = super.requestRectangleOnScreen(rectangle)
getFocusableGroupLayout()?.let { view ->
parentRect.set(
0,
0,
view.width,
view.height
)
view.requestRectangleOnScreen(parentRect, true)
}
return result
}
private fun getFocusableGroupLayout(): GroupFocusableLayout? {
var viewParent = parent
while (viewParent is View) {
if (viewParent is GroupFocusableLayout) {
return viewParent
}
viewParent = (viewParent as View).parent
}
return null
}
}
import android.content.Context
import android.util.AttributeSet
import android.widget.FrameLayout
class GroupFocusableLayout : FrameLayout {
constructor(context: Context) : super(context)
constructor(context: Context, attrs: AttributeSet?) : super(context, attrs)
constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(
context,
attrs,
defStyleAttr
)
}
@akexorcist
Copy link
Author

akexorcist commented Oct 6, 2021

Example

<com.akexorcist.keyboardfocus.GroupFocusableLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <LinearLayout
        android:layout_width="300dp"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            ... />

        <com.akexorcist.keyboardfocus.GroupFocusableEditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            ... />

        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            ... />
    </LinearLayout>

</com.akexorcist.keyboardfocus.GroupFocusableLayout>

Comparison

group-focused-small

Note

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