Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to get background color of button on android?

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..

like image 917
unbalanced Avatar asked Mar 23 '12 01:03

unbalanced


People also ask

How do I make the background a button color?

int colornumber=((ColorDrawable)v. getBackground()). getColor(); This is the best and simple way to get the color of a View (Button, TextView...)

How do I change the background color of my clicking button?

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.

What is default color of button in android?

When I open a new android studio project, the default color for button is purple.

Which button shows the background Colour?

The colour 2 button is used to show the background colour.


1 Answers

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();
like image 168
Laith Alnagem Avatar answered Sep 24 '22 09:09

Laith Alnagem