Karthik Balakrishnan
Karthik Balakrishnan

Reputation: 4383

Setting backgroundColor of relative layout

I'm trying to set a relative layout's background with,

relativeLayout1.setBackgroundColor(0x00000000);

My program keeps crashing though. Here's the logcat.

Code:

RelativeLayout window=(RelativeLayout) findViewById(R.id.window);
window.setBackgroundColor(0x00000000);

That's the only stuff apart from the regular code setContentView(R.layout.something); and super.onCreate(savedInstanceState);

Entire code:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_invisible);
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);
    window=(RelativeLayout) findViewById(R.id.window);
        window.setBackgroundColor(0x00000000);
}

Upvotes: 0

Views: 1059

Answers (4)

Karthik Balakrishnan
Karthik Balakrishnan

Reputation: 4383

This was another of Eclipse's moments. It didn't compile my code, so I was executing the old code. Restarted Eclipse and my app works fine now.

Upvotes: 0

Buneme Kyakilika
Buneme Kyakilika

Reputation: 1202

You can use this instead relative layout.setBackgroundDrawable(get resources().get drawable(R.drawable.bg);

Or you could define it in XML android: background="@drawable/bg"

Upvotes: 0

Pavel Dudka
Pavel Dudka

Reputation: 20934

From the logcat attached, I can say that most likely your window pointer is null at the time you are trying to set background color. It can be caused by different types of problems:

  • Your something.xml layout does NOT contain element with android:id="@+id/window" attribute
  • Your something.xml layout DOES contain element with android:id="@+id/window" attribute, but this element is not RelativeLayout
  • Your project resource data got messed up. Try to do Project->Clean to rebuild resources

Upvotes: 1

bazyle
bazyle

Reputation: 776

Is there is your R.layout.something a layout with the id R.id.window ?

Maybe you've missed something?

Upvotes: 0

Related Questions