Checking if a button has been clicked

50.1k views Asked by At

How to load menu depend on button was clicked? Any idea, solution?

@Override
public boolean onCreateOptionsMenu(final Menu menu) {

    menu.clear();
    if(button.**isclicked**) {
        getMenuInflater().inflate(R.menu.menu_main, menu);
    } else {
        getMenuInflater().inflate(R.menu.test, menu);
    }

    return true;
}
4

There are 4 answers

0
Aspicas On

Put a boolean field in your activity and change whenever you click button . Depends if its true or false show or not your menu

UPDATE

    //declare boolean
    boolean clicked=false;

    //my button clic
    button.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) 
    {
        //change boolean value
        clicked=true;
    }
    });

    //then on another method or where you want
    if(clicked)
   {
      openmenu();
   }
   else
   {
      closemenu();
   }
0
Vikram Rayavarapu On

try this function

button.isPressed();
0
olajide On

THe below also work fine.

if(button.isSelected())
0
AudioBubble On

Try something like this:

get the button's id

Button button = (Button) findViewById(R.id.button_send);

Adding click event listener

button.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
        //do whatever you want
    }
});