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.

helencrump
  • 1,351
  • 1
  • 18
  • 27
Wilson Christian
  • 650
  • 1
  • 6
  • 17

1 Answers1

0

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?

Community
  • 1
  • 1
TouchBoarder
  • 6,422
  • 2
  • 52
  • 60
  • I tried using it but failed to do so. Also it will only block the virtual home key right? @TouchBoarder – Wilson Christian Jan 15 '16 at 08:04
  • When I tried it blocked the hard home button, but this was on a Samsung with 4.1. I'll do a test later with a project I have and see if it still works? – TouchBoarder Jan 15 '16 at 11:13
  • @WilsonChristian updated the answer. I did test and it looks to not work above 4 anymore, but the other method I posted should. (It's not actually blocking the button, it's more like hiding the content with an overlay) – TouchBoarder Jan 16 '16 at 07:42
  • Your updated answer is not working with my design.I want both type of key i.e virtual and hardware for home,menu to be disable? Do you have anything regarding blocking of hardware home key? – Wilson Christian Jan 16 '16 at 08:36
  • @WilsonChristian This is what I know, I tested the 2nd option on Lollipop and it worked for me. please update your questions with code examples on what you have done so far. – TouchBoarder Jan 16 '16 at 09:57