I'm currently working on a project right now and felt like adding a pop-up window that would serve as the tutorial for the game. I watched this video which went through how you would go about doing something like this, and I managed to do it but unfortunately did not work in the end.
I put a setOnClickListener
on my tutorial pop-up button to basically bring up the window, but each time I pressed it, it would stop and go back to the main menu, indicating that it did not work.
The code:
package com.example.rockpaperscissors;
import androidx.appcompat.app.AppCompatActivity;
import android.app.Dialog;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import static com.example.rockpaperscissors.R.id.tutorialButton;
public class MainMenu extends AppCompatActivity {
Button tutorialButton, playGameButton, closeButton;
Dialog tutorial_popup;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
getSupportActionBar().hide();
setContentView(R.layout.activity_main_menu);
playGameButton = findViewById(R.id.playGameButton);
tutorialButton = findViewById(R.id.tutorialButton);
playGameButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(MainMenu.this, MainGame.class));
}
});
}
public void ShowPopup(View v) { <--The problem comes from here -->
tutorialButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
tutorial_popup.setContentView(R.layout.tutorial_popup);
closeButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
tutorial_popup.dismiss();
}
});
tutorial_popup.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
tutorial_popup.show();
}
});
}
}
I'm not exactly sure of what to do to fix it, so I would be happy to know where I went wrong in order to sort it out. Thank you in advance.