In this new episode, we are going to take care of the general menu of the game, which will appear after the presentation screen and will look like this:

02_-_MainMenu.jpg

We begin by creating in the directory « res / layout », a new file named « mainmenu.xml », in which we define the background of screen, the title and 4 buttons, in the following way:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
	android:orientation="vertical"
	android:gravity="center_horizontal"
	android:layout_width="fill_parent"
	android:layout_height="fill_parent"
	android:background="@drawable/main_screen">
	<ImageView
		android:src="@drawable/title_original_01"
		android:id="@+id/imageView1"
		android:layout_width="wrap_content"
		android:layout_height="wrap_content"/>
	<ImageView
		android:layout_width="wrap_content"
		android:id="@+id/imageView2"
		android:layout_height="75dp"/>
	<Button
		android:gravity="center_vertical|center_horizontal"
		android:layout_height="wrap_content"
		android:id="@+id/main_play_button"
		android:text="@string/main_play"
		android:layout_width="@dimen/main_button_width"
		android:onClick="main_play_button_onClick"/>
 	<Button
		android:gravity="center_vertical|center_horizontal"
		android:layout_height="wrap_content"
		android:id="@+id/main_options_button"
		android:text="@string/main_options"
		android:layout_width="@dimen/main_button_width"
		android:onClick="main_options_button_onClick"/>
	<Button
		android:gravity="center_vertical|center_horizontal"
		android:layout_height="wrap_content"
		android:id="@+id/main_highscore_button"
		android:text="@string/main_highscore"
		android:layout_width="@dimen/main_button_width"
		android:onClick="main_highscore_button_onClick"/>
	<Button
		android:gravity="center_vertical|center_horizontal"
		android:layout_height="wrap_content"
		android:id="@+id/main_quit_button"
		android:text="@string/main_quit"
		android:layout_width="@dimen/main_button_width"
		android:onClick="main_quit_button_onClick"/>
</LinearLayout>

We create a LinearLayout which will contain the various objects, with the attribute android:gravity positioned in center_horizontal, to center objects one under the other, and in which we put our background image "@drawable / main_screen". We add a first "ImageView" to show the image of the name of the game. We put a second "ImageView" empty to leave a space between the title and the buttons below. Then we define 4 buttons with attribute "android:onClick" informed with the name of the function managing the action of the button.

In « res/values »,, we add diverse definitions and diverse identifiers,

In « res/values/dimens.xml » :

<resources>
    <dimen name="gfont_en_font_h">18.0px</dimen>
    <dimen name="main_button_width">100.0dp</dimen>
    <dimen name="main_layout_y">80.0px</dimen>
</resources>

In « res/values/ids.xml » :

<resources>
    <item type="id" name="highscore_title">false</item>
    <item type="id" name="main_play_button">false</item>
    <item type="id" name="main_options_button">false</item>
    <item type="id" name="main_highscore_button">false</item>
    <item type="id" name="main_quit_button">false</item>
</resources>

In « res/values/strings.xml » :

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="app_name">Spacoid</string>
    <string name="main_highscore">High Scores</string>
    <string name="main_options">Options</string>
    <string name="main_play">Play</string>
    <string name="main_quit">Quit</string>
</resources>

We update the file AndroidManifest.xml to define the new activity which will open to show the main menu of the game.

<?xml version="1.0" encoding="utf-8"?>
<manifest
    xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.jeux.mainmenu"
    android:versionCode="1"
    android:versionName="1.0">
    <uses-sdk android:minSdkVersion="4" />
    <application
        android:icon="@drawable/icon"
        android:label="@string/app_name">
        <activity
            android:name="MainMenuActivity"
            android:label="@string/app_name"
            android:screenOrientation="portrait"
            android:theme="@android:style/Theme.NoTitleBar.Fullscreen">
            <intent-filter>
               <action android:name="android.intent.action.MAIN" />
               <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>
        <activity
            android:name="MainMenu" 
            android:screenOrientation="portrait"
            android:theme="@android:style/Theme.NoTitleBar.Fullscreen"/>
    </application>
</manifest>

Finally we create a new class which will post the menu and will manage the various buttons. In « src/com.jeux.mainmenu », we create the new file MainMenu.java, which will contain:

package com.jeux.mainmenu;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

public class MainMenu extends Activity {
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.mainmenu);
	}

	public void main_play_button_onClick(View v){}

	public void main_options_button_onClick(View v){}

	public void main_highscore_button_onClick(View v) {}

	public void main_quit_button_onClick(View v)
{
		finish();
	}
}

In the « onCreate » function, we show the layout " R.layout.mainmenu " corresponding to the display. Then we create a function for every callback associated with the present buttons in the menu:

main_play_button_onClick This function will be called on the selection of the button " Play " and will launch the game. main_options_button_onClick This function will be called on the selection of the button "Options" and will allow to reach the options of sound, RAZ of the score and some information on the game. main_highscore_button_onClick This function will be called on the selection of the button " High Scores " which will show the high scores screen. main_quit_button_onClick This function will be called on the selection of the button " Quit " which will end the activity and will leave the application.

download archive.