android - how to remove and add a button from the screen without affecting other buttons? -
i'm dynamically creating buttons , place them randomly on screen. want remove button when it's being clicked , create one, when that, other buttons (the ones bellow him percise) change position (moving 1 row). how can modify code other buttons wont change position when click button? want add new button in same row of 1 removed. i'm using linear layout, when add button adds bottom , fill blank row pushing rows.
in following code remove button after few seconds:
private void creategreenbutton() { random r = new random(); int hidedelay = r.nextint(1000) + 1000; final button greenbutton = new button(this); greenbutton.settext("button"); greenbutton.setonclicklistener(greenbuttonclick(greenbutton)); layout.addview(greenbutton, new linearlayout.layoutparams( linearlayout.layoutparams.wrap_content, linearlayout.layoutparams.wrap_content)); // place buttons randomly on screen layout.getviewtreeobserver().addongloballayoutlistener( new ongloballayoutlistener() { @suppresslint("newapi") @override public void ongloballayout() { random r = new random(); layout.getviewtreeobserver() .removeongloballayoutlistener(this); linearlayout.layoutparams layoutparams = (layoutparams) greenbutton .getlayoutparams(); int horizontalmargin = r.nextint(screen_width - greenbutton.getwidth()); layoutparams.setmargins(horizontalmargin, 50, 0, 0); greenbutton.setbackgroundcolor(color.green); greenbutton.setlayoutparams(layoutparams); } }); layout.requestlayout(); mhandler.postdelayed(new runnable() { public void run() { ((viewmanager) greenbutton.getparent()).removeview(greenbutton); creategreenbutton(); } }, hidedelay); }
Comments
Post a Comment