I am having a problem trying to get a Background color from a TextView and setting it as background of a button.
How can I solve that excepetion? And why is my conditions not correct?
My code is:
public void randomBotaoCorrecto() {
Random generator = new Random();
int rand = generator.nextInt(3);
TextView textElement = (TextView) findViewById(R.id.TVnomes);
int cor = textElement.getCurrentTextColor();
int cob1, cob2, cob3;
Button button11 = (Button) findViewById(R.id.button1);
Button button22 = (Button) findViewById(R.id.button2);
Button button33 = (Button) findViewById(R.id.button3);
ColorDrawable buttonColor1 = (ColorDrawable) button11.getBackground();
ColorDrawable buttonColor2 = (ColorDrawable) button22.getBackground();
ColorDrawable buttonColor3 = (ColorDrawable) button33.getBackground();
switch (rand) {
case 0:
button11.setBackgroundColor(cor);
do {
randomBotoes(button22);
cob2 = buttonColor2.getColor();
} while (cor == cob2);
do {
randomBotoes(button33);
cob3 = buttonColor3.getColor();
} while (cor == cob3 && cob3 == cob2);
break;
case 1:
button22.setBackgroundColor(cor);
do {
randomBotoes(button11);
cob1 = buttonColor1.getColor();
} while (cob1 == cor);
do {
randomBotoes(button33);
cob3 = buttonColor3.getColor();
} while (cor == cob3 && cob3 == cob1);
break;
case 2:
button33.setBackgroundColor(cor);
do {
randomBotoes(button22);
cob2 = buttonColor2.getColor();
} while (cor == cob2);
do {
randomBotoes(button11);
cob1 = buttonColor1.getColor();
} while (cor == cob1 && cob1 == cob2);
break;
}
}
and I am getting this as result:
Caused by: java.lang.ClassCastException: android.graphics.drawable.RippleDrawable cannot be cast to android.graphics.drawable.ColorDrawable
So, now I have:
public void randomBotaoCorrecto() {
Random generator = new Random();
int rand = generator.nextInt(3);
TextView textElement = (TextView) findViewById(R.id.TVnomes);
Drawable cor = textElement.getBackground();
int colorb1, colorb2, colorb3;
Button button11 = (Button) findViewById(R.id.button1);
Button button22 = (Button) findViewById(R.id.button2);
Button button33 = (Button) findViewById(R.id.button3);
ColorDrawable buttonColor1 = (ColorDrawable) button11.getBackground();
ColorDrawable buttonColor2 = (ColorDrawable) button22.getBackground();
ColorDrawable buttonColor3 = (ColorDrawable) button33.getBackground();
switch (rand) {
case 0:
button11.setBackground(cor);
colorb1 = buttonColor1.getColor();
do {
randomBotoes(button33);
randomBotoes(button22);
colorb2 = buttonColor2.getColor();
colorb3 = buttonColor3.getColor();
} while (colorb1 == colorb3 && colorb3 == colorb2 && colorb1 == colorb3 );
break;
case 1:
button22.setBackground(cor);
colorb2 = buttonColor2.getColor();
do {
randomBotoes(button11);
randomBotoes(button33);
colorb1 = buttonColor1.getColor();
colorb3 = buttonColor3.getColor();
} while (colorb2 == colorb3 && colorb3 == colorb1 && colorb1 == colorb2);
break;
case 2:
button33.setBackground(cor);
colorb3 = buttonColor3.getColor();
do {
randomBotoes(button11);
randomBotoes(button22);
colorb2 = buttonColor2.getColor();
colorb1 = buttonColor1.getColor();
} while (colorb3 == colorb1 && colorb1 == colorb2 && colorb3 == colorb2);
break;
}
}
But still the same result