I am trying to create a new app that would have on the main page with a vertical linear layout a button that says "ADD". After hitting add it would popup a window that asks for a name. After hitting ok it would create three buttons in a horizontal linear layout. The one on the left with the name that was entered and the two on the right that would say Delete and Edit. I have already created the basics of this programmatically with an if statement so each time the ADD button is pressed a new layout with the three buttons a new name is displayed one right below the previous. The problem that I am having is trying to add ids to these buttons and then accessing them from the oncreate bundle for the main method in order to do something. Should I be doing this programmatically or should I create 5 layouts each with 3 buttons and then hide them until the add button is pressed? A few other functions that I want to try to add is to be able to reorder the buttons and obviously delete them and create new ones by pressing add. Here is what I have so far. Thanks for any advice.


package com.th3ramr0d.armytrooptotask;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.text.Editable;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Toast;

public class MainActivity extends Activity {

public int count = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
Button addTroopBtn = (Button) findViewById(R.id.addTroop);
        
        addTroopBtn.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(count <= 5)
{
inputName(v);
}
else
{

}
}
});

    }
    
    public void inputName(final View v){
AlertDialog.Builder alert = new AlertDialog.Builder(this);

    //alert.setTitle("Enter Name and Rank");
    alert.setMessage("Please Enter A Name");

    // Set an EditText view to get user input 
    final EditText input = new EditText(this);
    alert.setView(input);

    alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int whichButton) {
     Editable name = input.getText();
     addTroop(name);
     }
    });

    alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
     public void onClick(DialogInterface dialog, int whichButton) {
       // Canceled.
     }
    });

    alert.show();
    }
    
    public void addTroop(Editable name){
   
    LinearLayout mainPage = (LinearLayout) findViewById(R.id.manageTroopsMain);
   

        //CREATE NEW LINEAR LAYOUT
        final LinearLayout addTroopLayout = new LinearLayout(this);
   
        //CREATE LAYOUT PARAMS FOR LAYOUT
        LinearLayout.LayoutParams newLayout = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
        newLayout.bottomMargin = 10;
        
        //STYLE NEW LINEAR LAYOUT
    addTroopLayout.setTag("addTroopLayout" + count);
    addTroopLayout.setLayoutParams(newLayout);
    addTroopLayout.setOrientation(LinearLayout.HORIZONTAL);    
        
    //CREATE NEW BUTTONS
    Button newTroop = new Button(this);
    Button remove = new Button(this);
    Button change = new Button(this);
   
    //CREATE LAYOUT PARAMS FOR BUTTONS
    LinearLayout.LayoutParams newTroopParam = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 20f);
    LinearLayout.LayoutParams rmvBtnParam = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, .5f);
    LinearLayout.LayoutParams chngNameParam = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, .5f);

   
    //STYLE NEW BUTTONS
    newTroop.setText(name);
    newTroop.setGravity(Gravity.LEFT|Gravity.CENTER_VERTICAL);
    newTroop.setLayoutParams(newTroopParam);
    if(count == 1){
        remove.setId(R.id.rmvBtn1);
    }
    if(count == 2){
    remove.setId(R.id.rmvBtn2);
    }
    if(count == 3){
    remove.setId(R.id.rmvBtn3);
    }
    if(count == 4){
    remove.setId(R.id.rmvBtn4);
    }
    if(count == 5){
    remove.setId(R.id.rmvBtn5);
    }
    remove.setText("-");
    remove.setGravity(Gravity.CENTER_HORIZONTAL|Gravity.CENTER_VERTICAL);
    remove.setLayoutParams(rmvBtnParam);
    change.setText("...");
    change.setGravity(Gravity.CENTER_HORIZONTAL|Gravity.CENTER_VERTICAL);
    change.setLayoutParams(chngNameParam);

    //ADD VIEWS TO NEW LAYOUT
    addTroopLayout.addView(newTroop);
    addTroopLayout.addView(remove);
    addTroopLayout.addView(change);
   
    //ADD NEW LAYOUT TO mainPage LAYOUT
    mainPage.addView(addTroopLayout);
   
    //Increment Counter
    count++;
    }

}