Ad

How To Remove A Dynamically Created EditText?

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.

Ad

Answer

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

public void removeEdit(){
    llPincode.removeView(etPincode);
}
Ad
source: stackoverflow.com
Ad