30

I want to change the background color of my Main-View (not a Button or a Text-View) just the real background which is usually black... I got this code:

view.setBackgroundColor(0xfff00000);

This is inside an OnClickListener, but it just changes the background of the Button.

moritzg
  • 4,266
  • 3
  • 37
  • 62

10 Answers10

67

Try creating a method in your Activity something like...

public void setActivityBackgroundColor(int color) {
    View view = this.getWindow().getDecorView();
    view.setBackgroundColor(color);
}

Then call it from your OnClickListener passing in whatever colour you want.

Squonk
  • 48,735
  • 19
  • 103
  • 135
  • I have a dialog interface open with a text box. I want to change the color when i click "OK". I tried this piece of code and it did not work. Is it because my view is right now on dialog interface rather than the main activity itself ? – Naveen Mar 12 '13 at 19:09
  • I figured it out. If anyone is in a similar situation, just do this in your function : LinearLayout main = (LinearLayout) findViewById(R.id.myScreenMain); main.setBackgroundColor(color); – Naveen Mar 12 '13 at 20:46
  • @Naveen : Sorry I didn't see your comments above. If you want to attract somebody's attention then start your comment with `@` and then the stackoverflow username in the way I did for you in this comment. It will then show up in the user's inbox. Glad you found a solution to your problem. – Squonk Apr 18 '13 at 23:40
10

i don't know if it's the answer to your question but you can try setting the background color in the xml layout like this. It is easy, it always works

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:orientation="vertical"

    android:layout_width="fill_parent"

    android:layout_height="fill_parent"

 android:background="0xfff00000"

  >


<TextView

    android:id="@+id/text_view"

    android:layout_width="fill_parent"

    android:layout_height="wrap_content"

    android:text="@string/hello"

    />



</LinearLayout>

You can also do more fancy things with backgrounds by creating an xml background file with gradients which are cool and semi transparent, and refer to it for other use see example below:

the background.xml layout

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
    <shape>
        <gradient
            android:angle="90"
            android:startColor="#f0000000"
            android:endColor="#ff444444"
            android:type="linear" />
    </shape>
</item>
</selector>

your layout

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:orientation="vertical"

    android:layout_width="fill_parent"

    android:layout_height="fill_parent"

 android:background="@layout/background"


    >


<TextView

    android:id="@+id/text_view"

    android:layout_width="fill_parent"

    android:layout_height="wrap_content"

    android:text="@string/hello"

    />



</LinearLayout>
youssoua
  • 802
  • 1
  • 11
  • 20
6

Just add this below one line code in the XML file of that corresponding activity:

android:background="@android:color/black" 

it will help you for sure.

Trygve Laugstøl
  • 7,440
  • 2
  • 36
  • 40
Rakesh Gujari
  • 899
  • 1
  • 8
  • 12
4

First Method

 View someView = findViewById(R.id.randomViewInMainLayout);// get Any child View

  // Find the root view
  View root = someView.getRootView()

  // Set the color
  root.setBackgroundColor(getResources().getColor(android.R.color.red));

Second Method

Add this single line after setContentView(...);

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

Third Method

set background color to the rootView

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FFFFFF"
android:id="@+id/rootView"
</LinearLayout>

Important Thing

rootView.setBackgroundColor(0xFF00FF00); //after 0x the other four pairs are alpha,red,green,blue color. 
Zar E Ahmer
  • 33,936
  • 20
  • 234
  • 300
1

You can also try and provide an Id for the main layout and change the background of that through basic manipulation and retrieval. E.g:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/hello"

Which can then be followed by accessing through R.id.hello.... Pretty basic and I hope this does help :)

Maciej Musialek
  • 56
  • 1
  • 14
0

I just want to add my 2 cents. I had the same goal (to change the background color from the .java class). But none of the above methods worked for me.

Issue was, that I set the background color inside the layout .xml file with android:background="@color/colorGray":

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@color/colorGray">

So I just deleted particular line:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

Now I (you) can set the color from the code with:

getWindow().getDecorView().setBackgroundColor(Color.GRAY);
Jake_3H
  • 372
  • 4
  • 14
0

Just go to the activity_main.xml file. You will see an attributes panel on the right hand side, there you will find an attribute called "background". You can set the color over there.

-1

Try this:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/colorPrimaryDark"


</androidx.constraintlayout.widget.ConstraintLayout>
    />
ChristianYami
  • 586
  • 1
  • 9
  • 17
YoutuberMX
  • 13
  • 1
  • 3
-1

You should write in xml where background i

android:background="@drawable/imgname"
Harshad Pansuriya
  • 20,189
  • 8
  • 67
  • 95
-1

if you put your full code here so i can help you. if your setting the listener in XML and calling the set background color on View so it will change the background color of the view means it ur Botton so put ur listener in ur activity and then change the color of your view

RizN81
  • 1,009
  • 15
  • 26