0

I am working on a small app and i have been working on the finishing touches but have encountered a logic problem i cannot seem to solve.

I have a function:

public void quitOrStartGame(View view){
    Button quitStart = (Button)findViewById(R.id.quit_startButton);
    Button reset = (Button)findViewById(R.id.resetButton);
    reset.setClickable(false);

    if (quitStart.getText() == "Quit") {
        gridGame.quit();
        importGrid();
        quitStart.setText("Start");
        reset.setClickable(false);
        } 
    else {
        gridGame.startGame();
        importGrid();
        quitStart.setText("Quit");
        reset.setClickable(true);
    }
}

That is designed to start up my game. It is a 5x5 game with x's and o's for an assignment, its not really important what the assignment is for my problem.

What i am attempting to accomplish is in the beginning when you first open the app you see two buttons, one that says reset, and one that says start. When you press start it starts the game and the button changes to a quit button, which will stop the game from running.

The thing is, i do not want the reset button to be clickable until after the start button has been pressed, which currently does not happen. The reset button does its normal function before start is even pressed.

HOWEVER: When you do press start, and then eventually quit, this does disable the reset button, but that process has to happen first. What i want is for the reset button to be disabled until start is pressed, and then be disabled again for the duration of the time after quit is pressed.

I hope i explained the problem well enough, thanks in advance for any help you fine folks can offer!

Geedubs123
  • 13
  • 1
  • 7
  • Didn't read entire question, but regarding `if (quitStart.getText() == "Quit") {` take a look at: [How do I compare strings in Java?](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – Pshemo Oct 23 '16 at 11:05
  • For whatever reason in android studio this works, i know i thought it was weird too but if you dont mind could you read the actual question? If not please dont comment on posts saying "i didnt read what you asked, but youre wrong here". Thats not helpful to anyone. – Geedubs123 Oct 23 '16 at 11:09
  • I am not Android developer. I don't even have it installed so I doubt that I will be able to quickly spot your problem. `== "..."` is standard code smell in Java so I pointed that out. It could work if you would compare string literals like `"foo" == "foo"` because they are stored in string pool. I assumed that `getText()` returns `new String(...)` so comparing it via `==` shouldn't work, unless that method is also interning returned string in pool. – Pshemo Oct 23 '16 at 11:18
  • Again, not the problem, it is working. Thanks for the "answer" but this does not help solve the actual problem. The problem does not really depend on android knowledge to be honest, but then again you didnt read it so how would you know? – Geedubs123 Oct 23 '16 at 11:27

2 Answers2

0

As far as I understand your problem, you want the reset Button to not be clickable at the start of your program. There are two ways to archive this:

  1. You call reset.setClickable(false); in an initializing method (a method that is called at the start of your programm). For example in the classes constructor.

  2. You change the style of your reset button to have clickable disabled. You set android:clickable to false in your XML layout

Mein Name
  • 527
  • 3
  • 14
  • yeah this is how i have it to start out. What i have figured out in this time since i posted is that in this part: – Geedubs123 Oct 23 '16 at 11:56
  • if (quitStart.getText() == "Quit") { gridGame.quit(); importGrid(); quitStart.setText("Start"); reset.setClickable(false); } – Geedubs123 Oct 23 '16 at 11:57
  • i am setting it to not be clickable but in the else statement, which runs first it is being set to clickable without checking if the start button has been clicked first – Geedubs123 Oct 23 '16 at 11:58
0

I don't have the entire code so I can't be sure when you call:

quitOrStartGame(View view)

but all you need to do is toggle the clickable of the restart button on the start/quit button onClickLisetner

   quitStart.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if (quitStart.getText() == "Quit") {
                gridGame.quit();
                importGrid();
                quitStart.setText("Start");
                reset.setClickable(false);
            }
            else {
                gridGame.startGame();
                importGrid();
                quitStart.setText("Quit");
                reset.setClickable(true);
            }
        }
    });

you should also move the findViewById to a location that is only called once. like the OnCreate Method. and save the buttons as a global parameter. https://groups.google.com/forum/#!topic/android-developers/_22Z90dshoM

Roee N
  • 502
  • 2
  • 4
  • 17