Search code examples
androidpython-2.7kivykivy-language

Forcing a Kivy widget's orientation to be landscape/portrait


I'm developing an app where I want one of ScreenManager's screen to be in landscape orientation. I don't want it to change to vertical by itself. As of now, what I learned is only buildozer.spec file can change the app's orientation. What I want is to change the widget's orientation. Is there any way to do this?


Solution

  • You can place a content of the screen on a scatter layout, and then rotate it:

    test.kv:

    ScreenManager:
    
        Screen:
            name: 'normal'
    
            Grid
    
        Screen:
            name: 'flipped'
    
            ScatterLayout:
                do_rotation: False
                do_scale: False
                do_translation: False
                rotation: 90
                pos_hint: {'center_x': 0.5, 'center_y': 0.5}
                size_hint: None, None
                size: root.height, root.width
    
                Grid
    
    
    <Grid@GridLayout>:
        cols: 1
    
        Button:
            text: 'normal'
            on_press: app.root.current = 'normal'
        Button:
            text: 'flipped'
            on_press: app.root.current = 'flipped'
    

    main.py:

    #!/usr/bin/env python2
    # -*- coding: utf-8 -*-
    from kivy.app import App
    
    
    class Test(App):
        pass
    
    
    Test().run()
    

    @edit There is also plyer's Orientation.