0

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.

javdromero
  • 1,850
  • 2
  • 11
  • 19
Thanos
  • 386
  • 2
  • 13
  • Where is the tutorial_popup initialization? E.G tutorial_popup = new Dialog(activity); – javdromero Mar 11 '21 at 01:55
  • Oh I need to add such? – Thanos Mar 11 '21 at 12:14
  • You do, remember that `Dialog tutorial_popup` is not enough, it's declared but null, you have to initialize in order so use it – javdromero Mar 11 '21 at 13:30
  • I declared it "tutorial_popup = new Dialog(this);" by testing it both in the public void class and the private onCreate. In both those instances, it did not work for some reason. I'll edit the code on the main question to show you where I made these edits – Thanos Mar 11 '21 at 15:29
  • Nevermind this is fixed, thank you for the help though – Thanos Mar 12 '21 at 18:05
  • Can you tell us what worked for you? – javdromero Mar 12 '21 at 18:18
  • I had done too much than what was needed to make the dialog pop up, so after watching several videos and getting help, I realised that all that was needed to make the pop up window come was to just use the button to "listen" for the click, then have the tutorial_popup setContent, getWindow and show. Furthermore, adding that extra closeButton listener messed things up more. – Thanos Mar 12 '21 at 18:23

0 Answers0