iv.setImageResource(R.drawable.icon);
this line is working good for me
can I pass a variable value instead of icon like if i have 10 images in drawable and i want to decide on runtime which image to show can i pass the value through a variable and work it like
String variableValue = imageName;
iv.setImageResource(R.drawable.variableValue);
Resources.getIdentifier() can solve your problem.
String variableValue = imageName;
iv.setImageResource(getResources().getIdentifier(variableValue, "drawable", getPackageName()));
You could use an array to define your drawables:
int[] images = new int[2];
images[0] = R.drawables.image1;
images[1] = R.drawables.image2;
And then you can use this array to set a image at runtime:
lv.setImageResource(images[i]);
Where is is in this case either 0 to point to the first image or 1 to point to the second image of the array.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With