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"));

- Android : Execute some code after back button is pressed
- Android is starting optimizing... app 1 of 1
- How to change Android Button Color using xml attribute and programatically using java
- SQLite with Android Easy to Understand Tutorial that covers Select, Insert, Update and Delete
- How to make Android EditText not editable
- Make Android TextView Clickable like Buttons
- Unable to establish connection to adb : Android Studio Error
- Android Constant and Resource Type Mismatches Lint
- Android Shared Preferences API tutorial
- Fixing Android unknown error 961 while downloading app
- DDMS files not found hprof-conv.exe
- How to reset eclipse layout
- Android Developers Bluetooth Tutorial
- Android Studio Native typeface cannot be made error
- 21 Useful Android Emulator Short-cut Keyboard Keys
- Multiline EditText in Android Example
- How to screenshot on Android?
- JavaScript : redirect page to other url
- Unable to load VM from snapshot. The snapshot has been saved for a different hardware configuration
- How To Disable Landscape Mode in Android Application
- Change Title text for Android Activity using java code
- Android : Class file collision: A resource exists with a different case
- Android Emulator Soft Back button action using Computer keyboard
- Device not compatible error Android Google Play Store
- ERROR x86 emulation currently requires hardware acceleration. Intel HAXM is not installed on this machine
- java.lang.NoClassDefFoundError android.support.v4.content.LocalBroadcastManager
- Disable Fading Edges Scroll Effect Android Views
- Android : Unable to load VM from snapshot : Mac OS X Error
- Change Android EditText Cursor Height
- What is Android Toast.LENGTH_SHORT and Toast. LENGTH_LONG durations
- How to add border to Android TextView
- Remove ActionBar from Activity that extends appcompat-v7
- How to send SMS on Android Emulator
- Android RatingBar Example
- This Toast was not created with Toast.makeText() : Android RuntimeException
- Android Studio Ctrl Shift o auto import not working
- Android : Duplicate registration for activity com.example.abc
- ADT Installation Error: requires plug-in org.eclipse.wst.sse.ui
- Running Android Lint has encountered a problem NullPointerException Error
- Android Emulator] ##KBD: Full queue, lose event Error Logs
- Export aborted because fatal lint error were found
- Android Parsing Data for android-L failed Unsupported major.minor version 51.0 Error
- Android : Accidental Octal Lint Warning
- Android ListView turns Black or Flickers while Scrolling
- How to make a dummy phone call from Android Emulator device
- Installation error: INSTALL_PARSE_FAILED_MANIFEST_MALFORMED - Android
- Create assets folder in Android Studio - Android-Studio
- Make Android TextView Clickable like Buttons - Android
- Add imports in eclipse shortcut key combinations - Eclipse
- SharePoint error cannot connect to the configuration database - SharePoint
- How to hide quick launch in SharePoint - SharePoint
- MySQL #6 - Error on delete of './my-database/db.opt' (Errcode: 13 - Permission denied) - MySQL
- 11 Weeks of Android Online Sessions-15-Jun-to-28-Aug-2020 - Android
- PHP header location function not called - PHP
- Compare two files in Notepad++ - NotepadPlusPlus
- AlertDialog with no buttons (just text message) - Android
- How to Save Eclipse console logs in external log file - Eclipse
- How to Enable spellcheck Notepad++ - NotepadPlusPlus
- Center align text in TextView Android Programming - Android
- ADT Installation Error: requires plug-in org.eclipse.wst.sse.ui - Android