Reputation: 143
This is my code:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my_activity2);
Button n = (Button) findViewById(R.id.button);
Typeface typeface = Typeface.createFromAsset(getAssets(), "BebasNeue Bold.ttf");
n.setTypeface(typeface);
final TextView tv = (TextView) findViewById(R.id.textView);
Typeface face = Typeface.createFromAsset(getAssets(),
"OSP-DIN.ttf");
tv.setTypeface(face);
final String[] values = getResources().getStringArray(R.array.things_array);
n.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Random RAND=new Random();
String nextValue = values[RAND.nextInt(values.length)];
tv.setText(nextValue);
}
});
}
I also have this:
<string-array name="colorcode_array">
<item>3498db</item>
<item>2ecc71</item>
<item>9b59b6</item>
<item>f1c40f</item>
<item>1abc9c</item>
<item>2980b9</item>
<item>8e44ad</item>
<item>e41c1c</item>
<item>2ecca9</item>
<item>752ecc</item>
<item>4f2ecc</item>
<item>2eccc3</item>
<item>2ecc53</item>
<item>2ecc2e</item>
<item>5bcc2e</item>
<item>9ecc2e</item>
<item>cca12e</item>
<item>cc712e</item>
<item>f1c209</item>
<item>86f109</item>
<item>f11616</item>
<item>9c1818</item>
</string-array>
Now what I would need is that the moment the button is clicked, you can see that the button already randomley loads text into the textview from things_array, but at the same time I want the background color to change randomly by using the color codes in colorcode_array.
How can I do this?
Upvotes: 1
Views: 762
Reputation: 209
view.setBackgroundColor(Color.parseColor("#"+array[index]));
should do it for you :)
(your case: view = tv
and array[index] = nextValue
Upvotes: 3