kvdroid / Kvdroid

Some Pyjnius tools for Kivy-Android developments.
MIT License
95 stars 24 forks source link

Keyboard height returning zero #33

Closed T-Dynamos closed 1 year ago

T-Dynamos commented 2 years ago

Code to reproduce error

from kvdroid.tools import keyboard_height

print(keyboard_height())

Returns 0

kengoon commented 2 years ago

@T-Dynamos your keyboard needs to be in view before it gets its height

T-Dynamos commented 2 years ago

@kengoon Not working can you send minimal working code with kivy?

brvier commented 2 years ago

+1 not working here also.

brvier commented 2 years ago
def keyboard_height():
    try:
        decor_view = activity.getWindow().getDecorView()
        height = activity.getWindowManager().getDefaultDisplay().getHeight()
        decor_view.getWindowVisibleDisplayFrame(Rect)
        return height - Rect().bottom
    except JavaException:
        return 0

Trashing errors is probably not a good things. Specially here, activity has no getWindow method.

T-Dynamos commented 2 years ago

I now use android module

import android
print(android.get_keyboard_height())
antorix commented 1 year ago

Hi @yunus-ceyhan, thanks for fixing this issue. But I have a small problem, I still see zero in keyboard_height() even after forceful keyboard creation within TextInput:

def create_keyboard(self, *args):
    self.show_keyboard()
    from kvdroid.tools import keyboard_height
    print(str(keyboard_height())

Maybe I installed the new version incorrectly? I pip-installed it first from .zip:

pip install https://github.com/kvdroid/Kvdroid/archive/refs/heads/master.zip

Then I added the same zip to the Buildozer spec file:

requirements = python3, kivy, plyer, jnius, https://github.com/kvdroid/Kvdroid/archive/refs/heads/master.zip

I also did buildozer android update.

Can you please tell me what I'm missing here and how to make the bugfix work?

yunus-ceyhan commented 1 year ago

@antorix Is Pydroid app installed on your Android device? If not please download it from the Play store. Instead of building your app every time we can test a simple example on Pydroid to see if it works or not.

Also you should call keyboard_height after keyboard appeared on your device otherwise it will always return 0

antorix commented 1 year ago

@yunus-ceyhan

@antorix Is Pydroid app installed on your Android device? If not please download it from the Play store. Instead of building your app every time we can test a simple example on Pydroid to see if it works or not.

I had some experience with Pydroid, but currently I work with Kivy. If you explain to me how to use Pydroid with Kvdroid, I will be glad to help. I didn't even know it supports third-party modules.

Also you should call keyboard_height after keyboard appeared on your device otherwise it will always return 0

It's exactly this way.

yunus-ceyhan commented 1 year ago

Install Pydroid, open the side menu, click on terminal then; pip install https://github.com/kvdroid/Kvdroid/archive/refs/heads/master.zip

Then test the code below.

from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.lang import Builder

Builder.load_string("""
#:import keyboard_height kvdroid.tools.keyboard_height

<Main>:
    orientation: 'vertical'
    Label:
        id: lb
        text:  f' Keyboard height: {keyboard_height()}'
        size_hint_y: None
        height: dp(70)
    TextInput:
        text: 'When you type here keyboard height should appear'
        on_text: lb.text =  f' Keyboard height: {keyboard_height()}'

""")

class Main(BoxLayout):
    pass

class MyApp(App):
    def build(self):
        return  Main()

if __name__ == '__main__':
    MyApp().run()
antorix commented 1 year ago

ok, it works, thanks again