Android Development

Creating Game Menus in Android

by Stephen Flockton on Sep.30, 2009, under how to, tutorial

As requested here is a sample tutorial in how to create a menu system for games in Android. Sorry for the delay but this tutorials take time to code test and write up. Anyway I hope you find it useful.

Before I jump into the code I’m going to take a second to explain my way of coding menus in Android. As we all know Android is built on the concept of activities. If you have been following earlier tutorials you already know how to create activities which can display graphics and deal with player input. But what if you want several different screens, such as options or credits? You could code them all into one activity but you would end up with a bloated and hard to maintain class.

The Android way of course is to split the screens in different activities. So you would have one for the options screen, one for the credits and of course one for the main game. This tutorial will show you how to create new activities via a menu with custom image buttons that change based on state.

Below is a screen shot of the menu application. Please forgive my terrible art skills but it is good enough to demonstrate custom button designs and how to create them yourself.

device

The net picture shows the button when in focus  but not clicked .

highlighted

And finally this picture shows the button pressed down.

Button highLighted

I apologise once more for the art skills but hey I’m a coder not an artist.

Each of the buttons in the example I’ve coded up will start a new activity. I’ve only added a simple text view and some basic text but in your games you would add whatever you wanted the screen to show. The exception is your main game activity which would load your graphics thread and start the game.

Now I’ve show you the eventual result of the tutorial we can jump into some code. We first need to create the XML layout to display the buttons. The Layout I made simply  creates four buttons in a table layout separated by some blank text views. The text views are simply to create a spacer between the buttons and are not used for anything else.

The Important parts of this code is the android:background fields for each button. The field itself specifies a background to use for the button. If you set this to point to a normal image the button would display it fine, however it would stay the same even when clicked or in focus.

In order to get it to change if clicked we need to tell it when to change and what image to change to. We do this in a separate XML file. So

1
android:background="@drawable/startgame_button"

Actually refers to a XML file called startgame_button.xml.

The XML file for the start game button is show below. It is created in the drawables folder and is a lot simpler then it looks. It defines four selectors which is the way android decides which image it should draw for the button.

1
2
3
4
5
6
7
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_focused="true" android:state_pressed="false" android:drawable="@drawable/startgame_highlighted" />
    <item android:state_focused="true" android:state_pressed="true" android:drawable="@drawable/startgame_pressed" />
    <item android:state_focused="false" android:state_pressed="true" android:drawable="@drawable/startgame_pressed" />
    <item android:drawable="@drawable/startgame" />
</selector>

The first selector is for when then button is focussed but not pressed. In this case I specified it should draw the startgame_highlighted image that is in the drawables folder. The second one is for if the button is focussed and pressed in which case it should draw the startgame_pressed image. The third is for when the button is not focussed but it pressed in which case we want to draw the startgame_pressed image. Finally there is the default case; in this case we only want to draw the normal startgame image.

Each button needs to have its own XML file to set which images to draw. Once that is done it is time to add code to the buttons to create the new activitys.We first need to create new classes to represent each new screen.

1
2
3
4
5
6
7
8
public class StartGame extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.startgame);
    }
}

Above is the code for the StartGame Activity. This is just an example and is the simplest activity it is possible to write. Its layout file simply specifies a text view and some basic text. In a real game it is here you would setup your custom views and graphics threads as explained in earlier tutorials. Each of the other screens also has an activity created for it in the same way and you would adjust the code in each activity to display what you wanted in each.

We now need to know how to start each of these activities. We do this in the Menu Class which in my case is the entry point for the app. Again this is as simple as it is possible to make the class. This class implements the onCreate() function which in turn creates four Button listeners for each of the buttons.  The code below shows the code for one of the listeners.

1
2
3
4
5
6
7
Button StartGameButton = (Button) findViewById(R.id.StartGame);
StartGameButton.setOnClickListener(new OnClickListener() {
    public void onClick(View v) {
        Intent StartGameIntent = new Intent(Menu.this,StartGame.class);
        startActivity(StartGameIntent);
    }
});

Firstly we create a reference to the Startgame Button using findviewById() to get the right button. We then define a new onClickListener. This basically is just a way of specifying what to do when you click the button. When the Startgame button is pressed it calls the associated onClick function.

The two most important lines in the code are show below.

1
2
Intent StartGameIntent = new Intent(Menu.this,StartGame.class);
startActivity(StartGameIntent);

This code creates the intent to start the activity and then calls the startActivity function to start it.The intent  construtor requires a refrence to the current class and the name of the class you want to start.You may wonder why we need to specify Intent to create an activity. We have to do this since android is an open platform and its possible to get another application to fulfil an activity that you need.

Obviously we want our activity to be called, so we create an intent which refers explicitly to the activity we want starting. I recommend reading the documentation about intents if this is confusing to you.

Finally we need to declare the activity’s in the application manifest file. So to add the four classes need for the menu we just need to add these lines to the manifest.

1
2
3
4
<activity android:name=".StartGame"></activity>
<activity android:name=".Help"></activity>
<activity android:name=".Options"></activity>
<activity android:name=".Credits"></activity>

That’s all there is too it. I would recommend taking a look at the sample code I’ve provided here which will hopefully make it make more sense. Fell free to add any questions in the comments and i’ll do my best to answer them.

Menu Tutorial Source

Stephen Flockton

  • Share/Bookmark
:, , , ,

14 Comments for this entry

  • John

    Just a comment to thank you for all your good tutorials.

    That is exactly what I was looking for.

    I hope you’ll continue to write a lot of android game development tutorials !

  • Amerant

    Thank you so much Stephen… Looking forward for more tutorials!

  • Ismael

    thank you!! I will read it now

  • sherlockhua

    thank you very much!!

  • Chris

    Hi Stephen

    I’m just starting to learn Android….thanks for the tutorial.

    How does this menu format fit in with the onCreateOptionsMenu methods for redisplaying the menu when the “Menu” button is pressed?

    Thanks!

  • Stephen Flockton

    Hi Chris,

    While you could override the menu button to start your menu activity I would not recomend it.

    Users within Android expect the menu key to open a context menu and not return to this menu. If you did the back key would then lead you back into your application causing all kinds of bad user expirences.

    If you need to return to this menu, I would create the option within your application itself and not the meny button.

    Hope this answers your question.

    Stephen

  • Chris

    Thanks Stephen!

  • papsic

    thanks man,
    yours all articles are good.
    Please continue.

    PAPSIC,BD

  • Nimou

    Hello and thx a lot for the tutorial!

    i would like to know if its possible to start a new activity from a view class, like something like that :

    1
    2
    3
    4
    5
    
    public class Menu extends Activity {
     @Override
    	protected void onCreate(Bundle savedInstanceState) {
    		super.onCreate(savedInstanceState);
    		setContentView(R.layout.cp1);
  • Nimou

    sry, i clicked “send” to soon by mistake.. here is the code i wanted to show..

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    
    public class Menu extends Activity {
      @Override
      public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(new myView(this));
      ...
      }
    }
    public class myView extends View{
      ...
      ...
      ...
      @Override
      public boolean onTouchEvent(MotionEvent event) {
        //start a new activity here for example ??
      }
    }

    thx

  • Martin

    Hm… never tried it because I always use a Listener to do that, but I don’t know a reason you shouldn’t be able to do this.

  • deep

    Thanks for such a nice tut…

  • hone

    thanks for the tutorial.

  • Rbads

    Hi is it possible to make a menu like share with facebook, twitter…which comes when we are in gallery and hit menu in android phone?

Leave a Reply