Greetings!

The main purpose of this website is to support Android indie game developers with tutorials, reviews and promotion of their games. The main difficulty for the developers is not to make the game, but to get exposure. To get it out there. If you find anything useful here, please spread the word. Like my page on Facebook, follow me on Google+ or Twitter. Thank you!

Google+ Facebook Twitter

× close

Tuesday, 22 October 2013

Tutorial - How to dim the software buttons

Many Android 4 phones nowadays have only software buttons (back, home, menu). Unlike hardware buttons they take up space on screen and also don't look very good in games. You might have noticed that it's not possible to hide the button bar completely.

You can hide the system bar using the following:
  1. Root the phone and force the system bar to hide - obviously you can't do this for every one of your players
  2. Hide buttons temporarily - they show again as soon as you touch the screen
  3. Dim buttons - this is called low profile mode

Low profile mode is very useful for games. It looks like this; left picture normal mode, right picture low profile:


This is how to safely implement it. We make sure that the method will be executed only on ICS and higher:

  @SuppressLint("NewApi")
  private void dimSoftButtonsIfPossible() {
    int currentapiVersion = android.os.Build.VERSION.SDK_INT;
    if (currentapiVersion >= android.os.Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
      getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LOW_PROFILE);
    } 
  }

Note: I am not sure what it does on phones with hardware buttons. If anyone could try and leave a comment - highly appreciated :)

And this is a good way how to use it in AndEngine:

  @Override
  public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK) {
      // do whatever you need here
      
      dimSoftButtonsIfPossible();
    }
    return false;
  }
  
  @Override
  public void onPopulateScene(Scene pScene,
      OnPopulateSceneCallback pOnPopulateSceneCallback)
      throws IOException {
    // do whatever you need here
    
    runOnUiThread(new Runnable() {
      
      @Override
      public void run() {
        dimSoftButtonsIfPossible();
      }
    });
    pOnPopulateSceneCallback.onPopulateSceneFinished();
  }
  
  @Override
  public synchronized void onResumeGame() {
    super.onResumeGame();
    // do whatever you need here
        
    runOnUiThread(new Runnable() {
      
      @Override
      public void run() {
        dimSoftButtonsIfPossible();
      }
    });

  }
  

Martin Varga is a Czech software developer who likes pygmy owls (hence Kulíš), running, ramen, travelling and living in foreign countries. He is also known as smartus or sm4 on the internet (read as smartass, but there are too many of them). He currently tries to make games in AndEngine like Mr. Dandelion's Adventures and hangs around a lot at the AndEngine forums.