Last active
October 8, 2021 00:14
-
-
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
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 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 | |
} | |
} |
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.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 | |
) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example
Comparison
Note