1 Reply Latest reply on Nov 11, 2013 11:43 PM by Kultida Chatarupachewin

    Disable Home button in an activity

    Pietro Maggi

       

      Given that in Android is not possible to intercept the Home button in the onKeyDown event.

      For example if try this code in an Android app, you never get the "Home button pressed!" log:

       

       
      @Override
      public boolean onKeyDown(int keyCode, KeyEvent event)
      {
          if(keyCode == KeyEvent.KEYCODE_BACK)
          {
              Log.d("Test", "Back button pressed!");
          }
          else if(keyCode == KeyEvent.KEYCODE_HOME)
          {
              Log.d("Test", "Home button pressed!");
          }
          return super.onKeyDown(keyCode, event);
      }
      
      
      

       

      You have two different possibilities:

      1. Use a intent-filter in the application manifest.xml (but this is not restricted to an application and Android show a list of the available home screens).
      2. Use a TYPE_KEYGUARD Layout Parameter for the Windows Manager.

       

      Intent-Filter

      Starting from an android application, you can add to the intent-filter section of the AndroidManifest.xml file some code to have it as:

       

       <intent-filter>
       
      <action android:name="android.intent.action.MAIN" />
       
      <category android:name="android.intent.category.LAUNCHER" />
       
      <category android:name="android.intent.category.HOME" />
       
      <category android:name="android.intent.category.DEFAULT" />
       
      </intent-filter>     
            
      

                                                                                                                                                       

      The problem with this solution is that you get a selection box  (till you select a default) that let you choose which intent you want to use.

       

      TYPE_KEYGUARD

       

      Add this code to your intent:

       

       @override
      
      public void onAttachedToWindow()
      {  
             this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);     
             super.onAttachedToWindow();  
      }
      
      
      
      

       

      Then you have to use for your activity the theme:

       

       android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
        
            
      

       

      This disable the Home Button and hide the notification bar.

       

      Pietro