Best Practice for grouping elements in TableLayout

+1 Matthew Cliatt · March 10, 2015
I'm making a sudoku app, and I have created a TableLayout with 81 individual TextView's to represent each square. 

Now the problem is, I would like the user to be able to select a single square, then select a number to be put into the square they selected.
Is there any way I can group them together, then get the individual TextView they clicked?

Maybe something like this:

TextView selected;
table.setOnClickListener(new TableLayout.OnClickListener() {
    selected = (TextView) table.getSelectedElement() ?

That way, when they choose a number I could just:

number.setOnClickListener(new Button.OnClickListener() {
    selected.setText( number.getText().toString() );

This way, I wouldn't have to make 81 separate onClick methods for each square, which I'm sure won't be the solution.

Thanks in advance for your help.

Post a Reply


Oldest  Newest  Rating
+1 Dol Lod · March 10, 2015
Create a class implementing one onClickListener. Add a member of this class as a listener. Make everything use the same listener. Listener takes in the individual view that was called so it avoids the entire issue altogether because it will only operate on the view it was called on. 
0 Matthew Cliatt · March 11, 2015
Good idea. I will implement it tomorrow and let you know the results. Thanks!
  • 1

Java / Android Development


Very popular language used to create desktop applications, website applets, and Android apps.

Bucky Roberts Administrator