0

I want to get the background color of a button inside a function. I want to check the equality of the color to perform some action. I use PaintDrawer but its not working.. app crashing... this is my function

void moves(Button bn)
{

    if(flag==1)
    {
       // Toast.makeText(getApplicationContext(),"Exception",Toast.LENGTH_LONG).show();


        if (bn.getText().equals("RED") && plyr==1 && mov==0|| mov==1 && bn.getText().equals("RED"))
        {


            rembr = bn;
            Toast.makeText(getApplicationContext(),"Exception"+rembr.getText(),Toast.LENGTH_LONG).show();
            mov = 1;
        }
        else if (bn.getText().equals("GREEN") && plyr == 2 && mov == 0 || mov==1 && bn.getText().equals("GREEN"))
        {
            Toast.makeText(getApplicationContext(),"Exception GREEN",Toast.LENGTH_LONG).show();

            rembr = bn;
            mov = 1;
        }



        else if (mov == 1 && bn.getText() != "RED" && bn.getText() != "GREEN")
        {
                /*check
                adjsnt(lbl, rembr);
                end check*/

            falsemov=adjsnt(bn, rembr);
            if (falsemov == 1)
            {
                falsemov = 0;

            }
            else
            {
                mov = 0;
                PaintDrawable drawable = (PaintDrawable) rembr.getBackground();

                int temp = drawable.getPaint().getColor();
                bn.setBackgroundColor(temp);
                bn.setText(rembr.getText());

                rembr.setBackgroundColor(Color.LTGRAY);
                if (plyr == 1)
                {
                    plyr = 2;
                    t1.setBackgroundColor(Color.LTGRAY);
                    t2.setBackgroundColor(Color.GREEN);
                }
                else
                {
                    plyr = 1;
                    t2.setBackgroundColor(Color.LTGRAY);
                    t1.setBackgroundColor(Color.RED);
                }
            }
        }
    }
}

Any help would be highly appretiated.

d4Rk
  • 6,622
  • 5
  • 46
  • 60
unniraja
  • 27
  • 5
  • 1
    Possible duplicate of [Get the background color of a button in android](http://stackoverflow.com/questions/8089054/get-the-background-color-of-a-button-in-android) – Laurent Meyer Jan 20 '16 at 18:02

1 Answers1

1

use Color Drawable class instead of paint

ColorDrawable buttonColor = (ColorDrawable) button.getBackground();

int colorId = buttonColor.getColor();

Community
  • 1
  • 1
Iram
  • 278
  • 2
  • 6