1

I have dynamic buttons that are created by a variable that can change.I want that these buttons have two functions. I did one option but I don't know how to implement the other option.

the first time I click the button I call a function that do something and the second time that I click the same button I would like to do another action. And I want to repeat this running with all the dynamic buttons created.

My code is:

 LinearLayout buttonsLayout = (LinearLayout)findViewById(R.id.linearlayoutUp);

         for(int i=0;i<drawView.getNumeroMallas();i++){

                Button buttonMalla = new Button(this);
                buttonMalla.setText("Malla "+(i+1));
                buttonMalla.setId(i+1);

                final int index = i;

                buttonMalla.setOnClickListener(new OnClickListener() {
                   public void onClick(View v) {

                       Malla malla = drawView.getMalla(index);
                       drawView.paintMallaSelected(malla);


                   }

                        }
                    });
                    buttonsLayout.addView(buttonMalla);

             }


    }
2
  • 1
    You want to change the text inside the button? "Select all" and "Deselect" ? Can you be more specific? Commented Feb 28, 2014 at 18:49
  • I edit my post with more information Commented Feb 28, 2014 at 18:57

1 Answer 1

2

EDIT - Very important:

I readed your code again, you could use the getTag/setTag to remember the last state of the button (i missed the for part, sorry!)

for(int i=0;i<drawView.getNumeroMallas();i++){

    Button buttonMalla = new Button(this);
    buttonMalla.setText("Malla "+(i+1));
    buttonMalla.setId(i+1);
    buttonMaila.setTag(Boolean.FALSE);

Then in setOnClickListener

if (((Boolean)v.getTag()) == Boolean.TRUE)
{
    // Do first action
    v.setTag(Boolean.FALSE);
}
else
{
    // Do second action
    v.setTag(Boolean.TRUE);
}

An idea could be to use a variable to know which was the last action.

A boolean variable

private boolean action = false;

If action is false do the first thing and set it to true. If it's true do the second action and set it to false. It should go out any method (global of the class)

Something like

private boolean action;
buttonMalla.setOnClickListener(new OnClickListener() {
    public void onClick(View v) {

        if (action == true)
        {
            // Do first action
        }
        else
        {
            // Do second action
        }

        action = !action;

        Malla malla = drawView.getMalla(index);
        drawView.paintMallaSelected(malla);


    }

}
});

Anyway if it does something of important you should manage better the button (example button action is not based on a boolean variable but in a specific state.) time ago i builded a "select all" and "unselect all" function in an app and i checked if the user have unselect manually something to let the button act again like a "select".. I hope i gave to you an idea.

Anyway the boolean variable is the most immediate way to do it.

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

1 Comment

It works perfectly. Only I have changed the first value buttonMaila.setTag(Boolean.TRUE);

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.