Skip to content
Related Articles
Open in App
Not now

Related Articles

How to Disable back button on android for kivy app ?

Improve Article
Save Article
Like Article
  • Difficulty Level : Expert
  • Last Updated : 21 Apr, 2021
Improve Article
Save Article
Like Article

In this article, we will develop a GUI window using kivy framework of python, and we will disable the android back button from the window.


  • When the kivy app starts the on_start() method will call automatically and this method will import EventLoop from kivy.
  • Using this EventLoop we will bind a method hook_keyboard() to the current window for detecting key presses means whenever we will press any key this hook_keyboard() method will call automatically, and it checks whether we have pressed back button (code for this back button is 27).
  • Then we are checking whether we are on the main screen or not if yes then we simply return True(and this app will get terminated) otherwise we are not returning anything and this program will not do anything on keypress.



# importing kivy App
from import App
# importing BoxLayout from kivy
from kivy.uix.boxlayout import BoxLayout
# importing ScreenManager and Screen from kivy
from kivy.uix.screenmanager import ScreenManager, Screen
class MainWindow(BoxLayout):
class uiApp(App):
    def on_start(self):
        from kivy.base import EventLoop
        # attaching keyboard hook when app starts
    def hook_keyboard(self, window, key, *largs):
        # key == 27 means it is waiting for 
        # back button tobe pressed
        if key == 27:
            # checking if we are at mainscreen or not
            if self.screen_manager.current == 'mainscreen':
                # return True means do nothing
                return True
                # return anything except True result in 
                # closing of the app on button click
                # if are not at mainscreen and if we press 
                # back button the app will get terminated
    def build(self):
        self.screen_manager = ScreenManager()
        # adding Designed screen to ScreenManager
        self.mainscreen = MainWindow()
        screen = Screen(name='mainscreen')
        # returning screen manager
        return self.screen_manager
if __name__ == "__main__":
    # running the object of app


This is the main design of the visible screen:


Note: This app wouldn’t do anything on running on pc it simply displays a screen on pc while the output of this app can be checked perfectly on an android device.

My Personal Notes arrow_drop_up
Like Article
Save Article
Related Articles

Start Your Coding Journey Now!