I have an ImageView
, in which I am programmaticly creating drawables and presenting them to the user. My goal is to click on said ImageView
and change the drawable's color.
How would I go about the random color changing bit? I am currently tinkering with Random()
, Color.argb()
and a few other things, but I can't seem to get it to work!
Random rnd = new Random(); int color = Color. argb(255, rnd. nextInt(256), rnd. nextInt(256), rnd.
Random rnd = new Random(); paint.setARGB(255, rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256));
or
Random rnd = new Random(); int color = Color.argb(255, rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256)); view.setBackgroundColor(color);
Though in your case it seems that you want to create a new drawable and assign it to your view. What is actually the drawable in your case? Is it an image, shape, fill...
to get random color values you can use this method:
public int getRandomColor(){ Random rnd = new Random(); return Color.argb(255, rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256)); }
then apply to your views:
myView.setBackgroundColor(getRandomColor());
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