Skip to content

Commit 7c8d4ce

Browse files
committed
Merge pull request kivy#360 from kived/faster-keyboard-height
use listener to respond to keyboard height changes
2 parents 884ad34 + 237e4ad commit 7c8d4ce

File tree

1 file changed

+17
-8
lines changed

1 file changed

+17
-8
lines changed

recipes/android/src/android/_android.pyx

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -171,24 +171,33 @@ cdef extern void android_show_keyboard(int)
171171
cdef extern void android_hide_keyboard()
172172

173173

174-
from jnius import autoclass
174+
from jnius import autoclass, PythonJavaClass, java_method
175175

176176
# API versions
177177
api_version = autoclass('android.os.Build$VERSION').SDK_INT
178178
version_codes = autoclass('android.os.Build$VERSION_CODES')
179179

180180

181181
python_act = autoclass('org.renpy.android.PythonActivity')
182-
rctx = autoclass('android.graphics.Rect')()
182+
Rect = autoclass('android.graphics.Rect')
183183
mActivity = python_act.mActivity
184184
if mActivity:
185-
decor_view = mActivity.getWindow().getDecorView()
186-
default_display = mActivity.getWindowManager().getDefaultDisplay()
187-
# get keyboard height
185+
class LayoutListener(PythonJavaClass):
186+
__javainterfaces__ = ['android/view/ViewTreeObserver$OnGlobalLayoutListener']
187+
188+
height = 0
189+
190+
@java_method('()V')
191+
def onGlobalLayout(self):
192+
rctx = Rect()
193+
mActivity.getWindow().getDecorView().getWindowVisibleDisplayFrame(rctx)
194+
self.height = mActivity.getWindowManager().getDefaultDisplay().getHeight() - (rctx.bottom - rctx.top)
195+
196+
ll = LayoutListener()
197+
python_act.mView.getViewTreeObserver().addOnGlobalLayoutListener(ll)
198+
188199
def get_keyboard_height():
189-
height = default_display.getHeight()
190-
decor_view.getWindowVisibleDisplayFrame(rctx)
191-
return height - rctx.bottom
200+
return ll.height
192201
else:
193202
def get_keyboard_height():
194203
return 0

0 commit comments

Comments
 (0)