Cannot change the color of a button manually

I have the following code :

Button x = (Button) findViewById(R.id.button1);
x.setBackgroundColor(Color.BLACK);

and I get the following error on the setBackgroundColor line :

Syntax error on token "setBackgroundColor", Identifier expected after this token

I'm trying to change the color code manualy, as it depends if the user has clicked on the button....

  • I do have a button with the id of "button1"

Thanks!

Answers


I think You have written this code out side of method which can not executed properly.You need to move this code inside some method

 public class SpinnerBuilding extends Activity {
    public void onCreate(Bundle state){

    super.onCreate(state);
    setContentView(R.id.layout);
    ...
    Button x = (Button) findViewById(R.id.button1);
    x.setBackgroundColor(Color.BLACK);
    }
    }

Try this code, it will work:

b.setBackgroundColor(getResources().getColor(R.color.red));

use this code:

x.setBackgroundColor(Color.parseColor("#000000"));//you can put hex code of any color inside the quotation.For black hex code is "000000"

Your code

Button x = (Button) findViewById(R.id.button1);
x.setBackgroundColor(Color.BLACK);

Must work.... but since you are getting

Syntax error on token "setBackgroundColor", Identifier expected after this token

that means your statement is outside any method but inside a class block. You can't put statements into a class declaration directly. you need to put them inside method.. As @Mukesh Kumar rightly pointed.


Need Your Help

Can't msdeploy web package to temporary folder?

asp.net msbuild msdeploy

We've got an Asp.Net web application we're trying to get pseudo-deployed to a folder, and I'm starting to think Microsoft are crazy -- why is it so hard to get a WAP to do a "Local FileSystem" depl...

merge two images into one image in iPhone

iphone objective-c uiimageview uiimage

I need to merge two images into one image, here is my code: