android - Add EditText dynamically with (if possible) String id into a Fragment -


i add edittext dynamically fragment. also, adding string id edittext. following code called after pressing button:

int number_of_edittexts; //at beginning=0 context context = getactivity(); edittext edittext = new edittext(context); edittext.setid("nofet"+number_of_edittexts); relativelayout.layoutparams params=new relativelayout.layoutparams(relativelayout.layoutparams.wrap_content,                         relativelayout.layoutparams.wrap_content);  params.addrule(relativelayout.center_horizontal); edittext.setlayoutparams(params); relativelayout rel=(relativelayout) getview().findviewbyid(r.id.list); rel.addview(edittext); number_of_edittexts++; 

it adds edittext, can't write edittext.setid("nofet"+numer_of_edittexts); edittext.setid(numer_of_edittexts);

is there way want? , also, how can params.addrule(relativelayout.below,r.id.dynamic_id)?

element ids pure integers, can't set strings. ids assigned elements created in xml converted integer internally , stored int.

dynamically created elements have id of -1 default. can manually assigned id through setid() there chance of collision other ids created automatically system.

to prevent such collision, method given in this answer may used manually assign id.

edit: basically, link says if have api level 17+, use view.generateviewid() else if manually, don't go above 0x00ffffff id these reserved statically created elements. other that, avoid conflicts among ids created through code.

however, in case of question, linearlayout may better way go.

suppose xml.

<linearlayout       android:id="@+id/list"       android:layout_height="wrap_content"       android:layout_width="fill_parent"       android:orientation="vertical"       android:gravity="center_horizontal"/> 

the java code add edittext may following:

list<edittext> edittexts; ... linearlayout rel=(linearlayout) getview().findviewbyid(r.id.list); ...  void addedittext(context mycontext,int edittextno) {     edittext ed=new edittext(mycontext);     ed.settext("edittext"+edittextno);     layoutparams lparamsmw = new linearlayout.layoutparams(layoutparams.match_parent, layoutparams.wrap_content);     ed.setlayoutparams(lparamsmw);     edittexts.add(ed);     rel.addview(ed); } 

you can modify created edittexts through list edittexts.

the vertical linearlayout automatically gives vertical list format required op. margin can added each added element if required. if required add more elements left or right of edittext, horizontal linearlayout may dynamically created, elements added , horiz. linearlayout added static 1 in similar manner in code above.


Comments

Popular posts from this blog

wordpress - (T_ENDFOREACH) php error -

Export Excel workseet into txt file using vba - (text and numbers with formulas) -

Using django-mptt to get only the categories that have items -