android - Buttons not showing when dynamically adding to LinearLayout -
i trying add number of buttons depending upon counter. have button increments counter. problem shows buttons in odd click. not show in clicks. example when click first, shows 1 button. click again, no button shows up. on third time, shows 3 buttons , on. can please tell me can issue. posting increment code , buttons displaying code.
<code> animalseasylayout = (linearlayout) findviewbyid(r.id.animalseasylayout); forward.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { // todo auto-generated method stub log.i("mustang", "increment"); increment(); showdigitimages(); } }); backward.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { // todo auto-generated method stub log.i("mustang", "decrement"); decrement(); showdigitimages(); } }); } private void increment() { counter++; digitview.settext("" + counter); if(counter >= 10) { forward.setenabled(false); } else { forward.setenabled(true); } } private void showdigitimages() { log.i("mustang", "show images counter: " + counter); animalseasylayout.removeallviews(); linearlayout.layoutparams layoutparams = new linearlayout.layoutparams(linearlayout.layoutparams.wrap_content, linearlayout.layoutparams.wrap_content); for(int count = 0; count < counter; count++) { final button b = new button(this); b.setid(count + 1); b.setlayoutparams(layoutparams); b.settext("child: " + (++count)); animalseasylayout.addview(b); } } private void decrement() { counter--; digitview.settext("" + counter); if(counter <= 0) { backward.setenabled(false); } else { backward.setenabled(true); } } </code>
you incrementing count
twice - once in for
statement , once inside loop while call settext
.
for(int count = 0; count < counter; count++) { final button b = new button(this); b.setid(count + 1); b.setlayoutparams(layoutparams); b.settext("child: " + (++count)); animalseasylayout.addview(b); }
use
b.settext("child: "+ (count + 1));
Comments
Post a Comment