How to change background color in android app
Asked Answered
G

20

273

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

Gauldin answered 1/5, 2010 at 3:57 Comment(0)
M
374

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

Megalopolis answered 1/5, 2010 at 5:36 Comment(6)
@Jonny the first two letters provide the alpha value.Megalopolis
Thank you, but isn't it easier just to use the hex value?Serve
it would be very easy, but bad practice :)Megalopolis
It is even easier to use android:background="@android:color/white" which is predefined and doesn't require you to add anything to strings.xml.Codon
Could you add the location /res/layout/activity_main.xml for adding the element android:background to the answer?Sloop
Just to clarify a bit. Giving the top-level layout element a margin will always show black in the margin. This answer shows how to set the background of the screen itself #10157316Cribriform
S
164

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));
Spannew answered 2/5, 2010 at 15:23 Comment(1)
This is the right answer because it has themes, I believe. A one-liner is NOT the simplest way, but a way to spread an entire style to everything is. Styles are how you achieve one-line with even complex changes. Also, styles work on preferences, unlike general attributes. (although still with a few oddities)Theravada
E
63

Simplest way

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

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

Expel answered 14/7, 2012 at 2:55 Comment(2)
and programmatically: context.getResources().getColor(android.R.color.white)Expel
thanks, that was actually what I was looking for. In my case I have to do it in Java.Fishbein
F
10

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!

Fishbein answered 30/8, 2014 at 15:46 Comment(0)
T
4

The simplest way is to add android:background="#FFFFFF" to the main node in the layout.xml or /res/layout/activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
   <TextView xmlns:android="http://schemas.android.com/apk/res/android"
       android:layout_width="fill_parent"
       android:layout_height="fill_parent"
       android:padding="10dp"
       android:textSize="20sp" 
       android:background="#FFFFFF">
   </TextView>
Turrell answered 16/1, 2014 at 16:9 Comment(1)
This is not a good idea, because it will result in unnecessary GPU overdraw (first, the window background will be drawn and then the TextView's background on top of it). See: curious-creature.com/docs/android-performance-case-study-1.htmlGame
W
4

This method worked for me:

RelativeLayout relativeLayout = (RelativeLayout) findViewById(R.layout.rootLayout);
relativeLayout.setBackgroundColor(getResources().getColor(R.color.bg_color_2));

Set id in layout xml

xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/rootLayout"
android:background="@color/background_color"

Add color values/color.xml

<color name="bg_color_2">#ffeef7f0</color>
Witty answered 21/12, 2015 at 2:49 Comment(0)
M
4

The best way using background with gradient as it does not increase app size of your app images are poison for android app so try to use it less instead of using one color as a background you can use multiple colors in one background. enter image description here enter image description here

enter image description hereenter image description here

Monaco answered 10/8, 2017 at 11:15 Comment(0)
Y
3

You can try this in xml sheet:

android:background="@color/background_color"
Yulma answered 11/7, 2016 at 5:28 Comment(0)
D
2

In Layout,to change background use this.

android:background="@color/your_color"

In Program can be use this. For eg: Texview background color

 TextView tvName = (TextView) findViewById(R.id.tvName);
 tvName.setBackgroundColor(getResources().getColor(R.color.your_color));
Dehydrate answered 20/7, 2016 at 10:25 Comment(0)
S
2
  1. go to Activity_Main.xml
  2. there are design view / and text view .
  3. choose Text view
  4. write this code up:

    android:background="@color/colorAccent"
    
Sayres answered 5/4, 2017 at 3:8 Comment(0)
P
2

This code can be useful for you:

android:background="#fff"
Presumable answered 10/8, 2018 at 13:20 Comment(0)
D
2

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

The question says Simplest Way, so here it is.

Set parentViewStyle in all your parent views. Like most parent view of your activity, fragment and dialogs.

<LinearLayout style="@style/parentViewStyle">

  ... other components inside

</LinearLayout>

Just put this style inside res>values>styles.xml

<style name="parentViewStyle">
    <item name="android:layout_height">match_parent</item>
    <item name="android:layout_width">match_parent</item>
    <item name="android:background">@color/white</item> // set your color here.
    <item name="android:orientation">vertical</item>
</style>

By this way, you don't have to change background color many times in future.

Deadhead answered 15/8, 2018 at 16:51 Comment(0)
P
1

you can try with this way

android:background="@color/white"
Popish answered 6/8, 2015 at 7:11 Comment(0)
R
1

You can use simple color resources, specified usually inside

res/values/colors.xml.

use

<color name="red">#ffff0000</color>

and use this via android:background="@color/red". This color can be used anywhere else too, e.g. as a text color. Reference it in XML the same way, or get it in code via

getResources().getColor(R.color.red).

You can also use any drawable resource as a background, use android:background="@drawable/mydrawable" for this (that means 9patch drawables, normal bitmaps, shape drawables, ..).

Rendering answered 4/10, 2016 at 5:19 Comment(0)
C
1

Some times , the text has the same color that background, try with android:background="#CCCCCC" into listview properties and you will can see that.

Cataract answered 16/12, 2016 at 16:7 Comment(0)
B
1

android:background="#64B5F6"
You can change the value after '#' according to your own specification or need depending on how you want to use them.
Here is a sample code:

<TextView
        android:text="Abir"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="24sp"
        android:background="#D4E157"  />

Thank you :)

Brython answered 26/3, 2017 at 6:7 Comment(0)
C
1

For Kotlin and not only, when you write

@color/

you can choose whatever you want, fast and simply:

android:background="@color/md_blue_900"
Cacuminal answered 15/8, 2019 at 17:23 Comment(0)
D
1

change_color.setBackground(getDrawable(R.color.purple_700));

This approach worked for when I tried changing colors on click.

Dealing answered 26/11, 2021 at 13:33 Comment(0)
V
0

Another way, go to layout -> your .xml file -> design view .Then go component tree and select layout you want to change color . In below component tree there is properties section .Select background in the properties section (In picture section 1). Then click section 2 in picture . Then Resources window will be open .From that select color menu .Then choose color you want .enter image description here

Vacuity answered 23/2, 2016 at 12:47 Comment(0)
M
0

If you would like to add background color to the entire activity

<RelativeLayout
    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"
    android:background="#1de9b6"
    tools:context="com.example.abc.myapplication.MainActivity">
 </RelativeLayout>

If you would like to use background for a view

 <TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Andrios"
    android:background="@color/colorAccent" />

Hope this helps!

Montemontefiascone answered 23/2, 2018 at 14:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.