Code2care : TechZone Apps & Tutorials

How to set background color for android layout programatically using java code and through XML



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 sub views like Buttons, TextView, EditText, ImageView e.t.c together.

Lets 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.



    1. 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 # 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>
    






2. Programatically 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 a color as we do using xml attribute in java using 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"));


Top




Posts related to android.


1. Share image and text Twitter using your Android Application Programatically

2. The Android Virtual Device 'myEmulator' is currently running an emulator and cannot be deleted.

3. Make Android View Scrollable both Horizontally and Vertically

4. Can we move apps like WhatsApp, Facebook to external MicroSD card

5. Parsing Data for android-21 failed unsupported major.minor version 51.0

6. Android : Prevent App for rotation landscape or portrait

7. ADT quit unexpectedly error on Mac OSX Android Eclipse SDK

8. Android-Failed to install apk on device EOF Timeout Error

9. How to change Android EditText Cursor Color

10. JavaDocs for Android SDK Eclipse IDE

11. Two Buttons next to each other in Android Layout

12. Change Title text for Android Activity using java code

13. How to add Newline to text in Android TextView

14. Android : Unable to load VM from snapshot : Mac OS X Error

15. INVALID FILE NAME: MUST CONTAIN ONLY [a-z0-9_.] Android Eclipse Error

16. Hide Tittle bar and Navigation bar from android activity

17. appcompat_v7 errors after updates to API level 21 Material Theme

18. How to Copy Text to Android Clipboard Programatically ClipboardManager (Both Old and New SDK)

19. [Solution] Running Android Lint has encountered a problem NullPointerException Error

20. Remove Trailing zeros BigDecimal Java

    more...






Popular tags
android
x 175
eclipse
x 29
notepadplusplus
x 20
macosx
x 14
sharepoint
x 14
html
x 14
mac
x 13
androidstudio
x 11




1000+ C Programs     PHP Tutorial     JSON Tutorial     Swift Tutorial     India Pinocdes     About Us     Privacy Policy


Code2care © 2012-17