android - generate dynamic id for multiple edittext with button click -
i have 1 row edittext
. scenario when user clicks on button row added. somehow have achieved both edittext
have same id. how assign id of edittext
dynamically created. edittext
in layout xml file. possible xml or have create edittext
programatically. in advance.
private void inflateeditrow(string name) { layoutinflater inflater = (layoutinflater) getsystemservice(context.layout_inflater_service); final view rowview = inflater.inflate(r.layout.row, null); final imagebutton deletebutton = (imagebutton) rowview .findviewbyid(r.id.buttondelete); final edittext edittext = (edittext) rowview .findviewbyid(r.id.req); if (name != null && !name.isempty()) { edittext.settext(name); } else { mexclusiveemptyview = rowview; deletebutton.setvisibility(view.visible); } // textwatcher control visibility of "add new" button , // handle exclusive empty view. edittext.addtextchangedlistener(new textwatcher() { @override public void aftertextchanged(editable s) { if (s.tostring().isempty()) { maddbutton.setvisibility(view.visible); deletebutton.setvisibility(view.visible); if (mexclusiveemptyview != null && mexclusiveemptyview != rowview) { mcontainerview.removeview(mexclusiveemptyview); } mexclusiveemptyview = rowview; } else { if (mexclusiveemptyview == rowview) { mexclusiveemptyview = null; } maddbutton.setvisibility(view.visible); deletebutton.setvisibility(view.visible); } } public void onaddnewclicked(view v) { // inflate new row , hide button self. inflateeditrow(null); v.setvisibility(view.visible); }
in order dynamically generate view id use form api 17
which generate value suitable use in setid(int)
. value not collide id values generated @ build time aapt r.id.
like this
@override public void onclick(view v) { // todo auto-generated method stub edittext edittext = new edittext(mainactivity.this); edittext.setid(edittext.generateviewid()); edittext.setheight(50); edittext.setwidth(50); ll.addview(edittext); }
Comments
Post a Comment