unbalanced
unbalanced

Reputation: 1202

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

Upvotes: 7

Views: 17370

Answers (1)

Laith Alnagem
Laith Alnagem

Reputation: 654

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();

Upvotes: 8

Related Questions