I want to get color of button.. I couldnt get color from getbackground function which returns drawable. I used getsolidcolor which returns integer value but its being 0 (zero) all time.. I dont understand where is problem. maybe its not true function..
here is my android code
int renk = btn1.getSolidColor();
if(renk== Color.GREEN)
Toast.makeText(getApplicationContext(), "green" , 1000).show();
else if(renk== Color.RED)
Toast.makeText(getApplicationContext(), "red" , 1000).show();
else if(renk== Color.YELLOW)
Toast.makeText(getApplicationContext(), "yellow" , 1000).show();
else
Toast.makeText(getApplicationContext(), "unknown", 1000).show();
btn1.setBackgroundColor(Color.YELLOW);
renk = btn1.getSolidColor();
if(renk== Color.GREEN)
Toast.makeText(getApplicationContext(), "green" , 1000).show();
else if(renk== Color.RED)
Toast.makeText(getApplicationContext(), "red" , 1000).show();
else if(renk== Color.YELLOW)
Toast.makeText(getApplicationContext(), "yellow" , 1000).show();
else
Toast.makeText(getApplicationContext(), "unknown", 1000).show();
I just get unknown toast message even I set background as yellow..
int colornumber=((ColorDrawable)v. getBackground()). getColor(); This is the best and simple way to get the color of a View (Button, TextView...)
Use HTML DOM Style backgroundColor Property to change the background color after clicking the button. This property is used to set the background-color of an element.
When I open a new android studio project, the default color for button is purple.
The colour 2 button is used to show the background colour.
Here ya go ....
Button myButton = (Button) findViewById(R.id.takePicture);
myButton.setBackgroundDrawable(new PaintDrawable(Color.YELLOW));
PaintDrawable drawable = (PaintDrawable) myButton.getBackground();
int color = drawable.getPaint().getColor();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With