How to change background color in android app

I want to be able to change the background color to white in my android app in the simplest way possible.


You need to use the android:background property , eg

android:background="@color/white"

Also you need to add a value for white in the strings.xml

<color name="white">#FFFFFF</color>

Edit : 18th Nov 2012

The first two letters of an 8 letter color code provide the alpha value, if you are using the html 6 letter color notation the color is opaque.

Eg :

enter image description here


You can also use

android:background="#ffffff"

in your xml layout or /res/layout/activity_main.xml, or you can change the theme in your AndroidManifest.xml by adding

android:theme="@android:style/Theme.Light"

to your activity tag.

If you want to change the background dynamically, use

YourView.setBackgroundColor(Color.argb(255, 255, 255, 255));

Simplest way

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

No need to define anything. It uses predefined colors in android.R.


To change the background color in the simplest way possible programmatically (exclusively - no XML changes):

LinearLayout bgElement = (LinearLayout) findViewById(R.id.container);
bgElement.setBackgroundColor(Color.WHITE);

Only requirement is that your "base" element in the activity_whatever.xml has an id which you can reference in Java (container in this case):

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:id="@+id/container"
              android:layout_width="match_parent"
              android:layout_height="match_parent">
     ...
</LinearLayout>

Paschalis and James, who replied here, kind of lead me to this solution, after checking out the various possibilities in How to set the text color of TextView in code?.

Hope it helps someone!