Android: Changing Background-Color of the Activity (Main View)
Solution 1
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.
Solution 2
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>
Solution 3
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.
Solution 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.
Solution 5
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 :)
Related videos on Youtube
Comments
-
moritzg over 2 years
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.-
David over 11 yearsLook at my answer here stackoverflow.com/a/12259787/592042
-
-
Naveen about 11 yearsI 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 about 11 yearsI 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);
-
Jim about 11 yearsEven if this didn't answer the OPs question, it did answer mine. +1
-
Jim about 11 yearsThat gradient background suggestion is awesome. Wish I could give you more than +1.
-
Squonk about 11 years@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. -
Naveen about 11 years@Squonk : Thanks much for the help. I'll keep that in mind :)