Monday, April 23, 2012

Override the Device's Back button

When browsing a game's Options Menu and decided to not modify the current settings,  you simply want to use your phone's Back button as, well, for going back to Main Menu.

But sadly, this is not the default behavior of that Back button.  When you try to create your game, pressing that button will quit (umm... hide, rather) your app.

But don't lose hope.  There's actually a way of changing that default behavior.

On your app's Activity class, add the following snippet.

public void onBackPressed() {
   if (state == STATE_MAINMENU) {
   } else {

      //do something else

Of course, both state and STATE_MAINMENU has to be defined for the snippet to be usable.  Also, whatever new behavior you want the Back button will have to do will be coded on the else part of the snippet, as indicated by do something else.

(via Creek Codes)

Saturday, April 21, 2012

Keep Screen in Portrait Mode

By default, your app will run in Portrait mode.  And you might opt to keep it that way, but would also rotate to Landscape mode if the phone it handled sideways.

To keep it in Portrait mode, open your AndroidManifest.xml then add the highlighted text below.

   android:screenOrientation="portrait" >
< /activity>

But if you are creating an Android game, chances are you wanted to default it in Landscape and stay that way. If so, just change portrait to landscape.

(via Creek Codes)