0

I'm new to java and unfortunately my brain is fixed in javascript land... This isn't good.

Anyways, I'm trying to just make a simple menu open and close using java. I have this script:

public class StartPage extends Activity {

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

        //Handlers
        RelativeLayout menuContainer = (RelativeLayout) findViewById(R.id.MenuContainer);
        ImageView menuButton = (ImageView) findViewById(R.id.temp_menu);
        Boolean menuOpen = false;
        //Make sure the menu is not displaying
        menuContainer.setVisibility(View.GONE);

        menuButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                if (!menuOpen) {
                    boolean menuOpen = true;
                    RelativeLayout menuContainer = (RelativeLayout) findViewById(R.id.MenuContainer);
                    menuContainer.setVisibility(View.VISIBLE);
                } else {
                    boolean menuOpen = false;
                    RelativeLayout menuContainer = (RelativeLayout) findViewById(R.id.MenuContainer);
                    menuContainer.setVisibility(View.GONE);
                }
            }
        });
    }
}

So, I make the variable menuOpen false right off the bat, but I want to use and manipulate that variable in an if statement.

I get an error in the if statement that says I need to make the variable final, so I make it final, but then I'm not able to change that variable later on.

This is my javascript brain thinking, so I'm not sure if there's a better way to do this in Java. Any help?

9
  • I'm 99% sure that you don't need if (menuOpen == false), you can just use if (!menuOpen) Commented Dec 2, 2015 at 21:58
  • Oh yeah, sorry - that's what I had before just if(!menuOpen), I was playing around a lot with it just as trial and error Commented Dec 2, 2015 at 21:58
  • 4
    Don't declare menuOpen inside a method, as it will go out of scope as soon as the method finishes. Try declaring it inside the class. Commented Dec 2, 2015 at 21:59
  • Yeah, keep track of your scope like @LukePark mentioned Commented Dec 2, 2015 at 21:59
  • 1
    Make menuOpen as member variable of the class StartPage. Then access it using StartPage .this.menuOpen inside anonymous class. Commented Dec 2, 2015 at 22:00

2 Answers 2

2

Make menuOpen as member variable of the class StartPage. Then access it using StartPage .this.menuOpen inside anonymous class.

public class StartPage extends Activity {
    boolean menuOpen = false;

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

        //Handlers
        RelativeLayout menuContainer = (RelativeLayout) findViewById(R.id.MenuContainer);
        ImageView menuButton = (ImageView) findViewById(R.id.temp_menu);
        //Make sure the menu is not displaying
        menuContainer.setVisibility(View.GONE);

        menuButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                if (!StartPage.this.menuOpen) {
                    StartPage.this.menuOpen = true;
                    RelativeLayout menuContainer = (RelativeLayout) findViewById(R.id.MenuContainer);
                    menuContainer.setVisibility(View.VISIBLE);
                } else {
                    StartPage.this.menuOpen = false;
                    RelativeLayout menuContainer = (RelativeLayout) findViewById(R.id.MenuContainer);
                    menuContainer.setVisibility(View.GONE);
                }
            }
        });

    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Added my working script to your answer, will accept in 7 minutes
0

Just put the menuOpen varible outside of the method.

public class StartPage extends Activity {

 Boolean menuOpen = false;
    @Override
    protected void onCreate(Bundle savedInstanceState) {

This way the listener's class has access to it.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.