Mark F
Mark F

Reputation: 1543

Using Mutliple Dynamically Created Buttons in Android

My program asks a user to enter their name and click on a button called btn. Once btn is clicked, their name is dynamically added to a TableRow along with another dynamically created Button. It's these Buttons that I'm having an issue with. I need to somehow access them later on in the program. I created a number of IDs in my res/value folder to keep track of each one(changebtn1, changebtn2, etc..). They're all stored in an array called buttonIDs.

Let's say that the user enters the first name, a new row is created with a dynamically created button:

 btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            tableRow = new TableRow(getApplicationContext());
            Button changeButton = new Button(getApplicationContext());

            changeButton.setText("Change");
            changeButton.setId(buttonIDs[i]);//From res/values

            tableRow.addView(changeButton);
            tableLayout.addView(tableRow);
            i++;
        });

Now let's say they enter a second name, another Button is created and so on and so forth. How can I now set an OnClickListener to my first Button that I created, which has the ID of R.id.changeBtn1? In other words, I have all of these dynamically created buttons and am not sure how to add OnClickLsteners() to earlier ones or access them in anyway. Thank you

Upvotes: 1

Views: 47

Answers (3)

Mark F
Mark F

Reputation: 1543

@filnik The first part of your answer also gave me an idea. I created an OnClickListener method outside of my OnCreate() method.

View.OnClickListener changeTeamName(final Button button)  {
    return new View.OnClickListener() {
        public void onClick(View v) {
            //Do Stuff
        }
    };
}

I then set an OnClickListener to EACH dynamically created Button and use the method that I created.

            changeButton.setText("Change");
            changeButton.setTag("ButtonOne");

            changeButton.setOnClickListener(changeTeamName(changeButton));

The fact that each Button now has an OnClickListener associated with it, they can now perform whatever function I add to my method.

Upvotes: 0

Filnik
Filnik

Reputation: 138

Or you attach the OnClickListener directly in the creation of the button or you can store the references to the buttons like this:

ArrayList<Button> buttons = new ArrayList<Button>();
btn.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        tableRow = new TableRow(getContext());
        Button changeButton = new Button(getContext());
        buttons.add(changeButton);

        changeButton.setText("Change");
        changeButton.setId(buttonIDs[i]);//From res/values

        tableRow.addView(changeButton);
        tableLayout.addView(tableRow);
        i++;
    });

for(Button button: buttons){
    button.setOnClickListener(new OnClickListener()
       ...etc...
    );
}

You won't waste a lot of memory since the buttons.add() line won't copy the button in the array but just the reference to the button. If you need a in id access to the buttons, use an HashMap, like this:

HashMap<String, Button> map = new HashMap<String, Button>();
map.put("id", new Button(getContext()));

And then access it like this:

Button button = map.get("id");

Upvotes: 1

rst
rst

Reputation: 2714

How about something like this

for(int i=0;i<buttonIDs.size();i++) {
   Button currentButton = (Button) findViewById(buttonIDs[i]);
   currentButton.setOnClickListener(new View.OnClickListener() {
       public void onClick(View v) {
          // Stuff
       }
   }); 
}

I did not check the code

Upvotes: 0

Related Questions