Every Android Activity class file has an associated layout.xml file to design the view. Every layout file has a parent View i.e. RelativeLayout, LinearLayout, TableLayout, TableRow, GridLayout e.t.c. that holds the subviews like Buttons, TextView, EditText, ImageView e.t.c together.
Let's see how we can set background color's to these Layouts with various options that we have,
How to set Background Color to Android Layout XML file!
- Using xml attributes
android:background="" is the attribute used to set background for any Layout file.
You can directly specify the value as HEX color code as we do for CSS files in HTML.
Example 1 : android:background="#FFFFCC"
You can also add transparency to the color by adding 2 more hex numbers after the # (hash) symbol.
Example 2 : android:background="#FFFFFFCC"
Example 3 : android:background="#00FFFFCC"
FF => Completely Opaque and 00 => Completely transparent.
You can also assign a color from color.xml resource file using @color/color
Example 4 : android:background="@color/lime_yellow"
File : res/values/color.xml
<?xml version="1.0" encoding="utf-8"?> <resources> <color name="lime_yellow">#FFFFEE</color> <color name="gray">#CCCCCC</color> </resources>
- Programmatically using Java code.
There are situations when you may want to change the background color of a layout using java code in your Activity.java file, then you can do it by using setBackgroundColor() method on your layout.
To your Parent View Layout add an attribute @id/id_name and map it to a variable in your java file.
Example 1: currentLayout.setBackgroundColor(Color.RED);
public class ColorActivityExample extends ActionBarActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_color_activity_example); //Set an id to the layout RelativeLayout currentLayout = (RelativeLayout) findViewById(R.id.main_layout); currentLayout.setBackgroundColor(Color.RED); }
Options for color available in Color class :
BLACK BLUE CYAN DKGRAY GRAY GREEN LTGRAY MAGENTA RED TRANSPARENT WHITE YELLOW
You can set rgb color code using method : Color.rgb(int red,int green,int blue);
Example 2 : currentLayout.setBackgroundColor(Color.rgb(200, 200, 200));
We can add Alpha to color as we do use XML attribute in java using the function : argb(int alpha, int red, int green, int blue);
Example 3 : currentLayout.setBackgroundColor(Color.argb(10,200, 200, 200));
If you wish to set the color code as HEX value you can do it using method Color.parseColor(String color);
Example 4 : currentLayout.setBackgroundColor(Color.parseColor("#FFFFFF"));

- Change Android Toast background color
- Maven : java.lang.ClassNotFoundException: Xmx512m
- This class should be public (android.support.v7.internal.widget.ActionBarView.HomeView) Lint Error
- Android Alert Dialog with Checkboxes example
- Android Error Generating Final Archive - Debug Certificate Expired
- How to add Newline to text in Android TextView
- Read Text file from SD Card : Android Programming
- [FIX] AndroidRuntime: FATAL EXCEPTION: main - java.lang.RuntimeException NullPointerException
- ActivityManager Warning: Activity not started, its current task has been brought to the front
- INSTALL_FAILED_INSUFFICIENT_STORAGE Android Error
- Android Developers Bluetooth Tutorial
- java.lang.ClassNotFoundException android.support.v7.widget.Toolbar [Fix]
- Android: Save Data in local Db using Android Room
- Channel 50 SMSes received every few minutes Android Phones
- 21 Useful Android Emulator Short-cut Keyboard Keys
- Changing Android Intent Tittle using java code
- Android : No Launcher activity found! Error
- How to change TextView or EditText Text Color on Focus and on Press
- How to display Toast on Button Click : Android
- Android : Execute some code after back button is pressed
- Stop android adb service from command prompt or terminal
- [Soluiton] You already have the latest version of Android Studio installed
- Create Custom Android AlertDialog
- Android R Cannot Be Resolved To A Variable
- How to make Android EditText not editable
- How to stop/start/restart apache server using command [Ubuntu] - Ubuntu
- Fix java.net.ProtocolException: Invalid HTTP method - Java
- JSON Nest Objects Example: JSON Tutorial - Json-Tutorial
- Install specific JRE on Ubuntu using apt - Ubuntu
- Synchronous XMLHttpRequest on the main thread is deprecated because of its detrimental effects to the end users experience - Java
- list of jars required for hibernate 4.x.x - Java
- The Android Virtual Device myEmulator is currently running an emulator and cannot be deleted. - Android
- Your Android SDK is out of date or is missing templates. Please ensure you are using SDK version 22 or later. - Android