I designed a chess board using 64 buttons in Android. But after designing it, I am facing many problems because I need to implement 64 OnClickListeners. Is there any other way to implement OnClickListener using a for loop, or it's not correct to design chess board using buttons?
- 20,171
- 8
- 62
- 72
- 35
- 4
-
1Just because 99% of examples use an anonymous subclass of `OnClickListener` doesn't mean you have to. Subclass it with a class that stores x, y in fields and a constructor with x, y as parameters. – martijno Mar 04 '13 at 17:38
3 Answers
You should take a look at the developer samples, especially the tic tac toe application which uses a custom view for a grid based game board. You should be able to expand upon that idea.
If you want to continue using buttons for each square, you will have to know the ids for them in order to create onClickListeners. There are two ways for you to solve this;
Either add the onClickListener in your layout xml:
<Button
parameters...
onClick="myOnClick"
</
In your Activity you can then create the method myOnClick:
public void myOnClick(View clickedView){
//code
}
A possibly better way for the button-solution would be to create them from code instead, either get your LayoutInflater and create your xml-buttons and add to your current content, or create new buttons using the corresponding java class: Button myButton e3 = new Button(context);
- 31,598
- 14
- 77
- 90
If you really want to have 64 separate views, you don't need 64 separate onClickListeners; just give the views an onClick attribute in the XML, all to the same method of the associated activity. You could then generate a table at build-time to map ids to coordinates, or you could add the coordinates to each view with your own attributes.
However, it would make much more sense to have a single view for your chess board. This view would partition itself into 64 squares, maintain an internal table indicating which pieces are where, draw the squares and the pieces in its .onDraw(), and do some math when clicked to determine which square was clicked on. Study TileView.java in the sample Snake app; once you understand that code, you'll have an easy time with your ChessboardView class.
- 5,459
- 17
- 29
Yes, it is possible to iterate through the buttons and add a listener to each. Inside your loop, you would do something like:
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Handle click events...
}
});
- 13,536
- 5
- 49
- 49
-
but each button has different name(like a1,a2..a8,b1,b2...b8),How it possible to use in for loop as a button id – Rajesh Tamire Mar 04 '13 at 17:40
-
This post has some good info on how to do that: http://stackoverflow.com/questions/4809834/how-to-iterate-through-a-views-elements – Eric Levine Mar 04 '13 at 17:51
-
I have created TableLayout with 8 tablerows, each tablerow containing 8 buttons in LinearLayout.so the answer given in that post will check all these childrens(including buttons) or not? – Rajesh Tamire Mar 04 '13 at 18:06
-
I don't know for sure, but you might just get the buttons and not the tablerows. Worst case, you'll get the tablerows, and then have to get the children of those. – Eric Levine Mar 04 '13 at 18:23