Reputation: 91
I am creating a number of buttons with a method and I would like to assign a string to an id to help me keep track of it. Right now I have this:
if (count <= 5)
{
//CREATE NEW BUTTONS
Button newTroop = new Button(this);
Button remove = new Button(this);
//STYLE NEW BUTTONS
newTroop.setId(count);
newTroop.setText("Button Number " + count);
remove.setId(count + 1);
remove.setText("-");
//CREATE NEW LINEAR LAYOUT
LinearLayout addTroopLayout = new LinearLayout(this);
//STYLE NEW LINEAR LAYOUT
addTroopLayout.setId(count);
addTroopLayout.setOrientation(LinearLayout.HORIZONTAL);
addTroopLayout.setBackgroundColor(Color.BLACK);
//ADD VIEWS TO NEW LAYOUT
addTroopLayout.addView(newTroop);
//ADD NEW LAYOUT TO mainPage LAYOUT
mainPage.addView(addTroopLayout);
//Increment Counter
count++;
}
I would like to change the line:
remove.setId(count + 1);
to
remove.setId("removeBtn" + count)
So each time the buttons are created they would be assigned removeBtn1, removeBtn2, etc etc. Is this possible or should I go about this another way.
Upvotes: 1
Views: 52
Reputation: 805
setTag()
is a good way to go. and you call getTag()
to get the name of the tag.
remove.setTag("removeBtn" + count);
Upvotes: 1