0

I want to remove my dynamically created edit text but i can not find that edittext id.. Basically I do not have any idea...

I used this code to create Edittext..

public void addEditText() {

    // add edittext
    etPincode = new EditText(mActivity);
    allEt.add(etPincode);
    LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
    p.setMargins(0, 5, 0, 0);

    etPincode.setLayoutParams(p);
    etPincode.setId(numberOfLines + 1);
    etPincode.setPadding(50,50,50,50);
    etPincode.setBackgroundResource(R.drawable.et_rectangular_noradius);
    etPincode.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_VARIATION_NORMAL);
    int maxLength = 10;
    InputFilter[] fArray = new InputFilter[1];
    fArray[0] = new InputFilter.LengthFilter(maxLength);
    etPincode.setFilters(fArray);
    Log.v("etttId1", String.valueOf(etPincode.getId()));
    Log.v("etttId2", String.valueOf(numberOfLines));

    llPincode.addView(etPincode);
    numberOfLines++;
}

and this code for retrieving values from edittext.

strings[] = new String[allEt.size()];
    for(int i=0; i < allEt.size(); i++){
        strings[i] = allEt.get(i).getText().toString();
        pincodeArray.add(strings[i]);
    }

    pincodes= String.valueOf(pincodeArray);

I want to to remove my edittext and also want to add one remove image on right of edittext.

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115

2 Answers2

0

You can remove it by using llPincode

if llPincode has only one child or you know the position then use:

llPincode.removeChildAt(position);

if llPincode has multiple views then use loop

foreach(int i=0; i< llPincode.toChildCount; i++){
    if(view instance of EditText){
        llPincode.removeChildAt(i)
    }
}
Sunny
  • 3,134
  • 1
  • 17
  • 31
0

As you already have a reference to etPincode saved outside addEditText(), you can simply do:

public void removeEdit(){
    llPincode.removeView(etPincode);
}