Menu System is an important part of a game, no matter which platform you want to target. Same is the case with Android and I found the platform flexible enough to facilitate any such requirement. While developing my first game, i too got into multiple approaches while i was searching for the best (and of-course ‘easier’ ) way to develop a menu system for my game. I found some ways on different forums that involve making an activity for each menu screen. This has some advantages but I personally didn’t like this approach. Finally I found a way, through some experimentation, that was more understandable (for an indie PC game developer, like me) and is described below:
1. Create a layout file (.xml) for your menu screen just like you do to create any other screen and place some controls on it. You can add as many screens as you want using this way.
2. Now you have to modify your game activity class to initialize these layouts using View objects. You can either keep separate variables for each view or you can declare and array of views and initialize them all.
3. The currentView variable is an index that determines the view that should be passed on to the activity to show on the screen.
4. Now initialize your view depending on its type. You must use the LayoutInflator class to get the view object that you want to store.
This is a simple utility function that takes a layout resource id and returns the required view object.
5. Write event handlers for your controls in each layout in your activity class and use the currentView variable to determine the next view to show.
6. Now you can use the setContentView(View) function in your event handlers to switch to the next screen by using currentView index and using it in allViews array. Look at the sample call below:
7. Optionally, you can pass parameters among these views by utilizing the view.setTag(object) function and passing it a custom parameter class instance.
This way, you can think of your whole game as a single activity (just like a PC game) and switching between screens as required. I don’t assume that this approach is the best one out there and i look forward for some positive feedback on this.