Reputation: 459
I am trying to compare color of buttons, I have assigned two colors android.R.color.holo_orange_dark
and android.R.color.holo_blue_dark
. It gives int
value in negative of background color of button.
Here is my code
public class MainActivity extends Activity implements OnClickListener{
private Button btn1;
private Button btn2;
private Button btn3;
private Button btn4;
private Button btn5;
private Button btn6;
private Button btn7;
private Button btn8;
private Button btnEmpty;
private ColorDrawable btnColor;
private int colorId;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ini();
btn1.setOnClickListener(this);
btn2.setOnClickListener(this);
btn3.setOnClickListener(this);
btn4.setOnClickListener(this);
btn5.setOnClickListener(this);
btn6.setOnClickListener(this);
btn7.setOnClickListener(this);
btn8.setOnClickListener(this);
}
/**
*
*
*/
private void ini() {
btn1 = (Button) findViewById(R.id.btn1);
btn2 = (Button) findViewById(R.id.btn2);
btn3 = (Button) findViewById(R.id.btn3);
btn4 = (Button) findViewById(R.id.btn4);
btn5 = (Button) findViewById(R.id.btn5);
btn6 = (Button) findViewById(R.id.btn6);
btn7 = (Button) findViewById(R.id.btn7);
btn8 = (Button) findViewById(R.id.btn8);
btnEmpty = (Button) findViewById(R.id.btnEmpty);
btnEmpty.setOnClickListener(this);
}
@Override
public void onClick(View btn) {
switch (btn.getId()) {
case R.id.btn1:
btnColor = (ColorDrawable) btn2.getBackground();
colorId = btnColor.getColor();
if (colorId == android.R.color.holo_blue_dark) {
Toast.makeText(MainActivity.this, "Color is holo_blue_dark", Toast.LENGTH_LONG).show();
}else{
Toast.makeText(MainActivity.this, "Color is holo_orange_dark", Toast.LENGTH_LONG).show();
}
break;
}
}
} }
where i am getting wrong??
Upvotes: 0
Views: 787
Reputation: 39191
The int
value of a color is not the same as the resource identifier for a color, and it may very well be a negative number. Try the following:
if (colorId == getResources().getColor(android.R.color.holo_blue_dark))
Upvotes: 1