java - Relative layout android loopable -


i writing app loads row of buttons (2 buttons black line underneath) each file found (so loop count not static). while building have static loop count 15. when running code creates bigger button on left , black line underneath fine... but... smaller button on right appears once. idea why?

    protected void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     scrollview scrollpictures = new scrollview(this);     relativelayout applayout = new relativelayout(this);    // applayout.setclipbounds(null);     resources r = getresources();     imageview blackline;      relativelayout.layoutparams p;     int id = 1;     for(int x = 1; x <= 15; x++){         p = new relativelayout.layoutparams(viewgroup.layoutparams.wrap_content,                 viewgroup.layoutparams.wrap_content);         button studentsbutton = new button(this);         studentsbutton.setclipbounds(null);         studentsbutton.setid(id);         studentsbutton.setheight((int) typedvalue.applydimension(typedvalue.complex_unit_dip, 100, r.getdisplaymetrics()));         studentsbutton.setwidth((int) typedvalue.applydimension(typedvalue.complex_unit_dip, 700, r.getdisplaymetrics()));         //studentsbutton.setbackgroundcolor(color.ltgray);         if (x > 1 ){             p.addrule(relativelayout.below, id - 1);             studentsbutton.setlayoutparams(p);         }          applayout.addview(studentsbutton);         id ++;          p = new relativelayout.layoutparams(viewgroup.layoutparams.wrap_content,                 viewgroup.layoutparams.wrap_content);         button soundbutton = new button(this);         soundbutton.setclipbounds(null);         soundbutton.setheight((int) typedvalue.applydimension(typedvalue.complex_unit_dip, 100, r.getdisplaymetrics()));         soundbutton.setwidth((int) typedvalue.applydimension(typedvalue.complex_unit_dip, 100, r.getdisplaymetrics()));         //soundbutton.setbackgroundcolor(color.ltgray);         p.addrule(relativelayout.right_of, id - 1);         soundbutton.setlayoutparams(p);         applayout.addview(soundbutton);          p = new relativelayout.layoutparams(viewgroup.layoutparams.wrap_content,                 viewgroup.layoutparams.wrap_content);         blackline = new imageview(this);         blackline.setid(id);         blackline.setbackgroundcolor(color.black);         blackline.setminimumwidth((int) typedvalue.applydimension(typedvalue.complex_unit_dip, 700, r.getdisplaymetrics()));         blackline.setminimumheight(3);         blackline.setminimumwidth((int) typedvalue.applydimension(typedvalue.complex_unit_dip, 700, r.getdisplaymetrics()));         blackline.setclipbounds(null);         p.addrule(relativelayout.below, id - 1);         blackline.setlayoutparams(p);         applayout.addview(blackline);         id++;       }      scrollpictures.addview(applayout);     setcontentview(scrollpictures);  } 

honestly, seems overkill in code. i'd recommend creating layout file, , inflating multiple times. make applayout linearlayout vertical orientation, simple this:

for(int x = 1; x <= 15; x++){   view view = layoutinflater.from(this).inflate(r.layout.some_layout, applayout, false);    // configure items inside view    button button1 = (button)view.findviewbyid(r.id.button1);   button1.settext("button 1");   button1.setonclicklistener(new view.onclicklistener() { ... });    ...    applayout.addview(view);  } 

and layout file relatively simple too:

<linearlayout android:orientation="vertical" ...>   <linearlayout android:orientation="horizontal" ...>     <button android:id="@+id/button1" ... />     <button android:id="@+id/button2" ... />   </linearlayout>   <view android:background="@android:color/black"         android:layout_height="3dp"         android:layout_width="match_parent" /> </linearlayout> 

while laying out elements in code do-able, can headache right. find easier use xml files, , inflate them needed this. guess way you're using ids confusing layout. it's best let buttons own id (don't use setid), , use "getid" method when setting relative layout params.


Comments

Popular posts from this blog

c# - Binding a comma separated list to a List<int> in asp.net web api -

Delphi 7 and decode UTF-8 base64 -

html - Is there any way to exclude a single element from the style? (Bootstrap) -