0

I have created an menu page for my app that has 4 buttons each with a different function. Before adding in this menu page my code looked like this in my activity_main.xml

  @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //sets full screen
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);

        //set no title
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);

        this.setContentView(new GameSurface(this)); 

Now I have this menu page with the 4 buttons which I have assigned in the MainActivity class to a variable in the following manner (only one shown for example purposes).

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button newGameBtn = findViewById(R.id.new_game_btn);

I then created the onclick listeners like so

newGameBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });

I have tried different methods within this to make the GameSurface class load (the class which before loaded the game) , it extends SurfaceView and implements SurfaceHolder.Callback. Any suggestions would be much appreciated.

Grilla99
  • 61
  • 1
  • 1
  • 6

2 Answers2

0

The GameSurface object is not in the layout hierarchy of the activity so this can be achieved in two ways as:

  1. Add you surface view in activity_main.xml

    <root>
        <packgename.GameSurface
            android:id="@+id/game_surfce_view"
        .../>
    </root>
    

then use

GameSurface gameView = findViewById(R.id.game_surface_view);
  1. You addView approach(layout param can be different for different view-groups) to add it at runtime through previous method is recommended for performance.
Pavneet_Singh
  • 36,884
  • 5
  • 53
  • 68
  • Hi and thanks for the response. I don't currently have an XML file for the game surface and is a class that draws a canvas with the characters in, is this method still possible? – Grilla99 Apr 02 '20 at 13:31
  • @Grilla99 The solution is the same as yours, I am not creating any additional activity, just adding the `packgename.GameSurface ` in `activity_main.xml` or any xml you want or use addview, both should work – Pavneet_Singh Apr 06 '20 at 09:21
0

To fix this issue I took the following steps.

In my MainActivity I added the following code:

public class MainActivity extends AppCompatActivity {
    private Context mContext = this;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button newScreen = (Button) findViewById(R.id.new_game_btn);
        Button exit = (Button) findViewById(R.id.exit_btn);

        newScreen.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mContext, GameActivity.class);
                mContext.startActivity(intent);
            }
        });

The GameActivity class included my aforementioned code that I had used in the MainActivity to run my surface view, shown below for reference.

//Launches a surface view

public class GameActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //sets full screen
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);

        //set no title
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);

        this.setContentView(new GameSurface(this));
    }
}

And my GameSurface class is that which extends SurfaceView and is where the logic of the game was contained. It should be noted that in order to do this I had to create an activity_game.xml file and link it with my game surface view like so:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" android:background="#0099cc" tools:context=".GameActivity">
    <com.example.fxgame.GameSurface
        android:id="@+id/mainview"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</FrameLayout>

This was still not working after implementing this however using the output log I discovered that I had not yet added the activity referenced from my MainActivity into my android manifest wherein activities are defined. This was rectified by adding the following line of code within the tag:

<activity android:name=".GameActivity" />

I spent a whole day goofing around with this so I hope that it aids someone else.

Grilla99
  • 61
  • 1
  • 1
  • 6