I have a grid of TextViews
shown inside a TableLayout
. When the user taps a TextView
I need to change the cell's background by adding an ImageView
that will go to another cell when other cells will be clicked.
Which is the best way to do this in Android?
Asked
Active
Viewed 1,336 times
2
-
1I didn't really understand your last sentence, can you describe it a little more? – C0deAttack Jan 18 '11 at 10:29
-
I need to move an ImageView to a certain position in a TableLayout ... how can i do it? – Cris Jan 18 '11 at 14:52
3 Answers
1
TextView can have an image (drawable) as a background. So it is easy to do it with textView.setBackgroundDrawable(R.drawable.myimage);
You can move the image into any cell-textView with this method and set the previous to another image or nothing with textView.setBackgroundDrawable(null);
You cannot animate this movement in the table, because each row is one view an may limit the animation of its children views to its constrains.

Lumis
- 21,517
- 8
- 63
- 67
0
Sounds like a puzzle you want do implemenent...
You should do the following to achieve what you want
- store all information you need from the first cell (which image is shown, which cell it was...)
- on the second cell store this information too
- now switch the image view settings, means you need to set the image of cell one to the image of cell 2 and vice versa

WarrenFaith
- 57,492
- 25
- 134
- 150
-
Hi, thanks for your answer; the problem is that i have 12x8 cells ... if i understand well you say to put an image hidden in every cell ... 96 images are too much to be handled in memory – Cris Jan 20 '11 at 12:10
-
Depends on the size of the images. I just thought about what you should do when you have two images you want to switch. If you just have one image that you want to be switch, the steps to do should be the same but you don't need to save information from the "destination" cell because you just override the imageview data there. Having 96 imageviews without an image should be possible (but maybe a bit slow). If you want to do a puzzle game or similar, I suggest to use a SurfaceView and draw images where you want. – WarrenFaith Jan 20 '11 at 12:15
0
This is a simple algorithm to solve your problem.
- Maintain an array of image ids which are displayed in table as same order.
Get two taps positions and swap the corresponding image ids in array and reload all images as like as array in table.
If u follow this u will get.

VenkaReddy
- 2,871
- 2
- 27
- 29