I want when a button is clicked to get it's background color.If the background color is the same with a color in resources named "blue_color", make background transparent.Else make it "blue_color". I have tried the method mentioned here but it gives me the error.
Code:
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ColorDrawable btc = (ColorDrawable) btn.getBackground();
int colorId = btc.getColor();
if(colorId == R.color.blue_color){
btn.setBackgroundColor(Color.TRANSPARENT);
}else{
btn.setBackgroundColor(getResources().getColor(R.color.blue_color));
}
}
});
error from logcat:
FATAL EXCEPTION: main java.lang.ClassCastException: android.graphics.drawable.InsetDrawable cannot be cast to android.graphics.drawable.ColorDrawable
in the line where is
ColorDrawable btc = (ColorDrawable) btn.getBackground();