Skip to content
Advertisement

Get imageView resource

i have been searching for hours, and cant seem to find an answer i have a chess board and its made of 64 imageViews (8X8) and i want to move one pawn to another square, and cant seem to find the way to get the image resource (which is a png image) and set it on the empty square. i saw answers to this question using .setTag, but i am already using it to define the position of the imageView on the board (for example the tag could be “4,4”)

a part of my code below: it shows the imageView and how im setting the resource.

i have another image view and i want to get the resource from the first one and onto the other one

img.setBackgroundResource(R.drawable.white_pawn_on_beige);

thanks in advance.

Advertisement

Answer

If you think you need to do this, you’re architecting your program incorrectly. You should have a model that knows what every square on the board has as a piece (if any). You should also have a function that can map a piece (or lack of a piece) to a resource id. You should simply update that model when a move is made, then redraw any effected squares from the model. You should not be using your views as state. Moving to an architecture like this should both make it easier to write and to test (instead of a complicated test involving views, you’re testing a function that updates the model, a function that maps the model to a resource for a given square, and a function that updates a model- no views involved).

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement