The fragment_game.xml
layout for the CannonGameFragment
contains a FrameLayout
that displays the CannonView
. A FrameLayout is designed to display only one View
—in this case, the CannonView
. In this section, you’ll create CannonGameFragment
’s layout and the CannonView
class. To add the fragment_game.xml
layout, perform the following steps:
1. Expand the project’s res/layout
node in the Package Explorer.
2. Right click the layout
folder and select New > Android XML File to display the New Android XML File dialog.
3. In the dialog’s File field, enter fragment_game.xml
4. In the Root Element section, select FrameLayout, then click Finish.
5. From the Palette’s Advanced section, drag a view (with a lowercase v) onto the design area.
6. The previous step displays the Choose Custom View Class dialog. In that dialog, click Create New... to display the New Java Class dialog.
7. In the Name field, enter CannonView
. In the Superclass field, change the superclass from android.view.View
to android.view.SurfaceView
. Ensure that Constructors from superclass is checked, then click Finish. This creates and opens CannonView.java
. We’ll be using only the two-argument constructor, so delete the other two. Save and close CannonView.java
.
8. In fragment_game.xml
, select view1 in the Outline window. In the Properties window’s Layout Parameters section, set Width and Height to match_parent
.
9. In the Outline window, right click view1, select Edit ID..., rename view1
as cannonView
and click OK.
10. Save fragment_game.xml
.