android - Adding/removing edit text and a remove (X) on click -
i new android development.
i want add edittext , remove button ( x ) when click on button (called “contact”). there multiple contacts. on click of remove button corresponding added edittext remove button should disappeared.
it this,
————————— x
————————— x
————————— x
————————— x
——————— |contact| ———————
if use adapter, how add empty edittext , remove button ( x ). if not other better options? need values entered in edittext.
note: on click of “contact” should adding edittext , remove button ( x ).
kindly give me clear , simple way this.
thanks!
create linearlayout , button. on button click call following method. also, create list in oncreate method track added edittexts & retrieve texts later.
private void createnewedittext() { edittext edittext = new edittext(this); edittext.setmaxlines(1); edittext.setsingleline(true); edittext.setlayoutparams(viewgroup.layoutparams(viewgroup.layoutparams.match_parent, viewgroup.layoutparams.wrap_content)); edittext.setcompounddrawableswithintrinsicbounds(null, null, vectordrawablecompat.create(resources, r.drawable.ic_clear_black_24dp, null), null); edittext.setontouchlistener(new view.ontouchlistener { @override boolean ontouch(view view,motionevent event) { val drawable_bottom = 3 val drawable_right = 2 val drawable_left = 0 val drawable_top = 1 if (event.getrawx() >= (edittext.right - edittext.getcompounddrawables()[drawable_right].bounds.width())) { edittextlist.remove(edittext); parentlinearlayout.removeview(edittext); return true; } return false; } }) edittext.requestfocus(); edittext.settext(voicetext); edittextlist.add(edittext); parentlinearlayout.addview(edittext); }
Comments
Post a Comment