Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: Generate random color on click?

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!

like image 791
Jared Avatar asked Mar 12 '11 03:03

Jared


People also ask

How to Generate Random colors in Android?

Random rnd = new Random(); int color = Color. argb(255, rnd. nextInt(256), rnd. nextInt(256), rnd.


2 Answers

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...

like image 169
Lumis Avatar answered Oct 11 '22 23:10

Lumis


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()); 

enter image description here

like image 35
Jorgesys Avatar answered Oct 11 '22 23:10

Jorgesys