I am starting to work with them ImageView
and I would like to know how to exchange funds between them dynamically. Suppose I have two ImageView
:
1)ImageView puesto_1_foto_1 = (ImageView) findViewById(R.id.puesto_1_foto_1);
two)ImageView mi_puesto_foto_1 = (ImageView) findViewById(R.id.mipuesto_foto_1);
What I would like to do is, once they are preloaded through java
, access the image itself that these ImageView
have defined as background
, in order to put the one of one to the other, and the one of the other to this first.
How to get the background/bottom image of a
ImageView
dynamically.What you would do is access the id of the uploaded image by defining the name in the tag:
By getting the reference
ImageView
you can get its id through its tag:
this id is the one you would use to load the image in another
ImageView
:Example:
According to the following layout we add 2 ImageView to which we want to exchange their images, for this in the tag, we add the name of the image ("android" and "robotito" ):
To exchange the images we need to obtain the id of the resource loaded in each ImageView, first we obtain the name of the resource and then its id, for this we use the getIdentifier() method :
when obtaining the id we obtain the Drawable from the id:
Now we can swap the images:
to get the following result:
Having the id of the resource in the system we can dynamically load another image,
You have to program an event that triggers the change, it is commonly used on clicklistener, that is, every time you click the imageView, you can change it as follows:
And you do something similar for imageview two: