Skip to content Skip to sidebar Skip to footer

Properly Resize Main Kivy Window When Soft Keyboard Appears On Android

I'm trying to use Window.softinput_mode to resize the window content when the soft keyboard appears: softinput_mode = 'resize' With this mode, the window is resized (i.e., window

Solution 1:

The problem is solved by puting

ifsmode== 'resize':
        y += kheight

in function Window.update_viewport(), I added the next code to my app in order to workout. DO NOT MUST CHANGE THE KIVY LIBS DIRECTLY

defupdate_viewport(self=Window):
    from kivy.graphics.opengl import glViewport
    from kivy.graphics.transformation import Matrix
    from math import radians

    w, h = self.system_size
    if self._density != 1:
        w, h = self.size

    smode = self.softinput_mode
    target = self._system_keyboard.target
    targettop = max(0, target.to_window(0, target.y)[1]) if target else0
    kheight = self.keyboard_height

    w2, h2 = w / 2., h / 2.
    r = radians(self.rotation)

    x, y = 0, 0
    _h = h
    if smode == 'pan':
        y = kheight
    elif smode == 'below_target':
        y = 0if kheight < targettop else (kheight - targettop)
    if smode == 'scale':
        _h -= kheight
    if smode == 'resize':
        y += kheight
    # prepare the viewport
    glViewport(x, y, w, _h)

    # do projection matrix
    projection_mat = Matrix()
    projection_mat.view_clip(0.0, w, 0.0, h, -1.0, 1.0, 0)
    self.render_context['projection_mat'] = projection_mat

    # do modelview matrix
    modelview_mat = Matrix().translate(w2, h2, 0)
    modelview_mat = modelview_mat.multiply(Matrix().rotate(r, 0, 0, 1))

    w, h = self.size
    w2, h2 = w / 2., h / 2.
    modelview_mat = modelview_mat.multiply(Matrix().translate(-w2, -h2, 0))
    self.render_context['modelview_mat'] = modelview_mat
    frag_modelview_mat = Matrix()
    frag_modelview_mat.set(flat=modelview_mat.get())
    self.render_context['frag_modelview_mat'] = frag_modelview_mat

    # redraw canvas
    self.canvas.ask_update()

    # and update childs
    self.update_childsize()


if __name__ == '__main__':
    app = MainApp()
    Window.update_viewport = update_viewport
    Window.softinput_mode = 'resize'
    app.run()

Have in count the import of Window and depending onn the kivy version kheight variable may have a different name.

Post a Comment for "Properly Resize Main Kivy Window When Soft Keyboard Appears On Android"