i already asked question (http://stackoverflow.com/questions/12663443/add-delete-option-with-dynamically-generated-edittext). I successfully implemented the answer,but the issue with this code is textEdit, spinner and the remove button together is not removing from the view. It happens but i have to made three button click to happen this .. Please go through my code .
btnAddNew.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
LinearLayout rAlign = (LinearLayout)findViewById(R.id.lId);
final EditText newPass = new EditText(getApplicationContext());
allEds.add(newPass);
newPass.setHint("Name of Label");
newPass.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
//newPass.setWidth(318);
newPass.setTextColor(Color.parseColor("#333333"));
newPass.setId(MY_BUTTON);
System.out.println(MY_BUTTON);
//newPass.setOnClickListener(this);
rAlign.addView(newPass);
addSpinner();//Code to add spinner
Button btnRemoveOld = new Button(getApplicationContext());
btnRemoveOld.setId(MY_BUTTON); // arbitrary number
rAlign.addView(btnRemoveOld);
btnRemoveOld.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
int idf =MY_BUTTON -1;
LinearLayout rAlign = (LinearLayout)findViewById(R.id.lId);
rAlign.removeView(findViewById(idf));
allEds.remove(newPass);
}
});
MY_BUTTON ++;
}
});
If you use MY_BUTTON to assign an ID for each View
You could do this in the btnRemoveOld’s onClickListner to remove all three objects
By doing v.getId(), you’re getting the id value of the btnRemoveOld and from the code you’ve assured yourself that the spinner is the id-1 and that the newPass button is the id-2.