android - Set buttons ids with Custom Adapter -
which better way create vertical lineal layout 4 or more buttons in each row
the problems have face following:
- setting id of each button manually result in lot of repetitive code, more resources usage , have change add feature or change (i think using adapter efficient way, but...)
- from know using customadapter don't set unique id buttons
can use adapter set different id each button dipending of row?
example:
second button of third row: r3b2 fifth button of first row: r1b5
thanks.
you can create buttons programmatically in our activity class this
linearlayout layout = (linearlayout) view.findviewbyid(r.id.layout); // layout in u want display buttons layout.setorientation(linearlayout.vertical); int count = 1; (int = 0; < 5; i++) { // = row count linearlayout row = new linearlayout(getactivity()); row.setlayoutparams(new linearlayout.layoutparams(linearlayout.layoutparams.match_parent, linearlayout.layoutparams.wrap_content)); (int j = 0; j < 7; j++) { // j = column count (create 7 buttons in each row) int id = count; final button btntag = new button(getactivity()); linearlayout.layoutparams params = new linearlayout.layoutparams(linearlayout.layoutparams.match_parent, linearlayout.layoutparams.match_parent); params.weight = 1; params.gravity = gravity.center; btntag.setlayoutparams(params); btntag.setbackgroundcolor(color.parsecolor("#ffffff")); string datesuffix = count + getdaynumbersuffix(count); btntag.settag(datesuffix); btntag.setid(id); btntag.settext(count + ""); btntag.settextsize(12.0f); btntag.setonclicklistener(getonclickdosomething(btntag, count)); row.addview(btntag); count++; } layout.addview(row); } view.onclicklistener getonclickdosomething(final button btntag, final int count) { return new view.onclicklistener() { public void onclick(view v) { // here can u want on button click } }
Comments
Post a Comment