-1

I want to change the background colour of my main activity using Java code. Help me with some code snippets!!

Bishan
  • 15,211
  • 52
  • 164
  • 258
arj
  • 5
  • 1
  • 4
  • Possible duplicate of so many questions like http://stackoverflow.com/questions/4761686/how-to-set-background-color-of-activity-to-white-programmatically – Kaidul Jul 21 '13 at 09:39
  • Dear they suggest using xml and buttons. I want to do it in oncreate – arj Jul 21 '13 at 09:50

2 Answers2

1
getWindow().getDecorView().setBackgroundColor(int) (if you are setting it to a color)

//This function also can be used to Views;

Example :

getWindow().getDecorView().setBackgroundColor(Color.RED); 

// background set to RED. MUST BE USED ONLY IN MAIN THREAD!

KOTIOS
  • 11,177
  • 3
  • 39
  • 66
  • the method setBackgroundColor(int) needs to be called. Which variable should be used to do that – arj Jul 21 '13 at 09:33
  • @PANDA i m using eclipse Helios.this.setBackgroundColor(Color.RED); is not available in onCreate() – arj Jul 21 '13 at 09:42
  • is Color xml available in res folder? if not use for example .setBackgroundColor(Color.parseColor("#bdbdbd")) – KOTIOS Jul 21 '13 at 09:43
  • Sorry. I have a mistake and updated a answer. See above. –  Jul 21 '13 at 09:46
  • @Stacks28 No Color xml in res... I want to change through Java code – arj Jul 21 '13 at 09:46
  • .setBackgroundColor(Color.parseColor("#bdbdbd")) use whatever color u want search color code appropriately – KOTIOS Jul 21 '13 at 09:49
  • @PANDA Thanks!! it worked. But what if i want to use thread which keep changing my screen background color – arj Jul 21 '13 at 09:56
0

If you want permanently changing color of you're Activity you must implementing two things :

1)Handler
2)Thread which cnanging color
Handler h = new Handler();

in Handler implements handleMessage method. In handleMessage Method use

getWindow().getDecorView().setBackgroundColor(Color.RED);//or msg.what

in run method use h.sendMessage(0); //or Color int value 0 in that case is a msg.what

Unfortunately i do not have on hand IDE to give you full example.