Android: Generate random color on click?
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...
How to set random color
You can generate random colors like this:
Random rnd = new Random();
int color = Color.argb(255, rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256));
How to output a random color from a set of selected colors in Java? (Android)
First of all in color.xml define your colors and create array of it.
<?xml version="1.0" encoding="utf-8"?>
<resources>
<item name="blue" type="color">#FF33B5E5</item>
<item name="purple" type="color">#FFAA66CC</item>
<item name="green" type="color">#FF99CC00</item>
<item name="orange" type="color">#FFFFBB33</item>
<item name="red" type="color">#FFFF4444</item>
<item name="darkblue" type="color">#FF0099CC</item>
<item name="darkpurple" type="color">#FF9933CC</item>
<item name="darkgreen" type="color">#FF669900</item>
<item name="darkorange" type="color">#FFFF8800</item>
<item name="darkred" type="color">#FFCC0000</item>
<integer-array name="androidcolors">
<item>@color/blue</item>
<item>@color/purple</item>
<item>@color/green</item>
<item>@color/orange</item>
<item>@color/red</item>
<item>@color/darkblue</item>
<item>@color/darkpurple</item>
<item>@color/darkgreen</item>
<item>@color/darkorange</item>
<item>@color/darkred</item>
</integer-array>
</resources>
Now generate random color like below in onCreate
method.
int[] androidColors = getResources().getIntArray(R.array.androidcolors);
int randomAndroidColor = androidColors[new Random().nextInt(androidColors.length)];
Lastly set this generated color.
view.setBackgroundColor(randomAndroidColor);
Source taken from here.
How to set background to a random color at button press in Android?
I'm not sure if this will work (but it's worth a try):
Try initializing color = new Random() within the onClick() statement.
b.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
color = new Random();
p.setARGB(256,color.nextInt(256),color.nextInt(256),color.nextInt(256));
a.setBackgroundColor((p.getColor()));
}
});
Also, look at this question:
Android: Generate random color on click?
it seems like it's trying to accomplish a similar goal.
How can set my button a random color to the background. Android
in the MainActivity code this :
public class MainActivity extends AppCompatActivity {
private ConstraintLayout layout;
private Button randombutton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
layout = findViewById(R.id.layout);
randombutton = findViewById(R.id.button);
randombutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int randomColor = new Kleurenpalet().getRandomColor();
layout.setBackgroundColor(randomColor);
}
});
}
}
Creating random colour in android with some key (for the same key it should generate same colour)
int getUserColourCode(String userId) {
StringBuilder input1 = new StringBuilder();
input1.append(userId);
input1=input1.reverse();
String pair[]={"0","0","0","0","0","0","0","0"};
char[] character = input1.toString().toCharArray();
for(int i=0;i<character.length;i++)
{
pair[i]=String.valueOf(character[i]);
}
int color = Color.argb((Integer.parseInt(pair[0]+pair[1])*2)+50, (Integer.parseInt(pair[2]+pair[3])*2)+50, (Integer.parseInt(pair[4]+pair[5])*2)+50, (Integer.parseInt(pair[6]+pair[7])*2)+50);
return color;
}
Android: Set Random colour background on create
In colors.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<item name="blue" type="color">#FF33B5E5</item>
<item name="purple" type="color">#FFAA66CC</item>
<item name="green" type="color">#FF99CC00</item>
<item name="orange" type="color">#FFFFBB33</item>
<item name="red" type="color">#FFFF4444</item>
<item name="darkblue" type="color">#FF0099CC</item>
<item name="darkpurple" type="color">#FF9933CC</item>
<item name="darkgreen" type="color">#FF669900</item>
<item name="darkorange" type="color">#FFFF8800</item>
<item name="darkred" type="color">#FFCC0000</item>
<integer-array name="androidcolors">
<item>@color/blue</item>
<item>@color/purple</item>
<item>@color/green</item>
<item>@color/orange</item>
<item>@color/red</item>
<item>@color/darkblue</item>
<item>@color/darkpurple</item>
<item>@color/darkgreen</item>
<item>@color/darkorange</item>
<item>@color/darkred</item>
</integer-array>
</resources>
In onCreate()
int[] androidColors = getResources().getIntArray(R.array.androidcolors);
int randomAndroidColor = androidColors[new Random().nextInt(androidColors.length)];
view.setBackgroundColor(randomAndroidColor);
How to choose a random color from a list in Kotlin?
You can define a color array and use random()
func to get random color from it
val colors = arrayOf(
Color.parseColor("#FFFFFF"),
Color.parseColor("#000000"),
Color.parseColor("#FF8F00"),
Color.parseColor("#EF6C00"),
Color.parseColor("#D84315"),
Color.parseColor("#37474F"),
//...more
)
val randomColor = colors.random()
Or random generate a color
val rnd = Random.Default //kotlin.random
val color = Color.argb(255, rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256))
Related Topics
Keep a Service Running Even When Phone Is Asleep
Is 'Shouldoverrideurlloading' Really Deprecated? What How to Use Instead
How to Resolve "Waiting for Debugger" Message
Error Message:This Android Sdk Requires Android Developer Toolkit Version 22.6.1 or Above
Firebase @Propertyname Doesn't Work
Can't Stop the Ringing Alarm from Another Activity
Fragment in Viewpager Using Fragmentpageradapter Is Blank the Second Time It Is Viewed
Importing Google-Play-Services Lib into Intellij Idea 12 (And 13)
Disconnect a Bluetooth Socket in Android
Edittext Maxlines Not Working - User Can Still Input More Lines Than Set
Android - R Cannot Be Resolved to a Variable
Pathpattern to Match File Extension Does Not Work If a Period Exists Elsewhere in the File Name
How to Include a Username When Storing Email and Password Using Firebase (Baas) in an Android App
How to Store an Integer Array in Sharedpreferences