android - Change a different button when i click a button -
i trying create memory game can't find way change x
button when press y
button. here code. want able handle different buttons when private void gridbuttonclicked
been used.
public class mainactivity2 extends actionbaractivity { private static final int num_rows =10 ; private static final int num_cols = 4; button buttons[][]=new button[num_rows][num_cols]; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main_activity2); populatebuttons(); } private void populatebuttons() { tablelayout table = (tablelayout) findviewbyid(r.id.tableforbuttons); for(int row=0; row < num_rows; row++){ tablerow tablerow=new tablerow(this); tablerow.setlayoutparams(new tablelayout.layoutparams( //we use fill screen tablelayout.layoutparams.match_parent, tablelayout.layoutparams.match_parent, 1.0f)); table.addview(tablerow); for(int col=0; col < num_cols; col++){ final int final_col=col; final int final_row=row; button button= new button(this); button.setlayoutparams(new tablerow.layoutparams( //we use fill screen tablerow.layoutparams.match_parent, tablerow.layoutparams.match_parent, 1.0f)); button.settext("" + col + "," + row); //text every button button.setpadding(0,0,0,0); //this make text visible if buttons small button.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { gridbuttonclicked(final_col,final_row); //we need final because can't pass variables inner classes } }); tablerow.addview(button); buttons[row][col]=button; } } } private void gridbuttonclicked(int col, int row) { toast.maketext(this,"clicked",toast.length_short).show(); button button=buttons[row][col]; // button.setbackgroundresource(r.drawable.abc_ab_share_pack_mtrl_alpha); string id= (string) button.gettext(); if(id.equals("1,2")) { button.settext("" + col + ""); //change button } if(id.equals("1")) { button.settext("" + col + "," + row); //change button } if(id.equals("2,1")) { button.settext("" + col + ""); //change button } }
any appreciated.
my suggestion create own view (you can extends button or imagebutton well) , using adapter fill gridview.
afterwards can control behaviour or view on class.
Comments
Post a Comment