Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change background color in android app

Tags:

android

People also ask

How do you change the color of your apps on Android?

Search for and select Open App, and then, on the New Shortcut page, tap Choose. Locate the app whose appearance you want to change. Back on the New Shortcut page, you'll see the app name; tap More (three dots), change the app's name, tap its icon, select Color, and choose a new color.


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!


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>

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>

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