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.

Related posts

Recent Viewed