Search code examples
androidandroid-homebutton

How to disable Home/Menu button in Android above 4.0?


I am developing a lockscreen application, and so far have achieved everything needed for the app to be working.

But I can't disable the home/menu buttons available as virtual as well soft in Android devices. I have gone through every possible answer on SO and other sites but can't achieve it.

Is there any tested and working workaround? Thanks in advance.


Solution

  • One way is to display a dialog where the LayoutParams type is set to TYPE_SYSTEM_ERROR and set the owner of this Dialog to your "lockscreen" Activity to block the home button.

    Here is an example on how this can be done: Update: looks like this only works with pre Android 4.+ https://github.com/Joisar/LockScreenApp/blob/master/LockScreenApp/src/com/mehuljoisar/lockscreen/utils/LockscreenUtils.java

    Another way is to add your contentView directly to the WindowManager where the LayoutParams type is set to TYPE_SYSTEM_ERROR

    ...
    onCreate(){
    
        //setContentView(R.layout.main_content);
        //instead add a View directly to the WindowManager
        View contentView = View.inflate(this, R.layout.main_content, null);
        LayoutParams lockLayoutParams = new LayoutParams();
                lockLayoutParams.width = LayoutParams.MATCH_PARENT;
                lockLayoutParams.height = LayoutParams.MATCH_PARENT;
                lockLayoutParams.type = LayoutParams.TYPE_SYSTEM_ERROR;
    
                //LOCK
                getWindowManager().addView(contentView, lockLayoutParams);
    
    ...
                //UNLOCK
                getWindowManager().removeView(contentView);
    

    The downside I had with this approach is that it looks to not support more complex views. I got a lot of flicker with ListViews in Fragments etc.

    Some example projects where this is used:

    https://github.com/Chin-Z/ArcherKeyguard/blob/master/src/com/lovewuchin/app/archerkeyguard/util/LockLayer.java

    https://github.com/radames/ScreenLock-Android/blob/master/src/com/eva/me/mysquarescreenlock/lockutil/LockLayer.java

    More answers to similiar question here: How to disable Home and other system buttons in Android?