Set Background Color of View in Android

The easiest way to set the background color of any view in Android.


Libraries

Code Snippet
LinearLayout linearLayout = (LinearLayout) findViewById(R.id.yourView);

// Using Colorcode
linearLayout.setBackgroundColor(0xFF00FF00);
// Using defined color
linearLayout.setBackgroundColor(Color.GREEN);
// Using resourcefile
linearLayout.setBackgroundColor(ContextCompat.getColor(getContext(), R.color.myGreen));


Implementation

Use one of the ways above to change the bacgroundcolor of the LinearLayout with id yourView to green. It is always recommend to use a resourcefile for such values.





Comments
  • mode_editWrite a comment



  • forumComments




Embed Code

We provide special containers which include the full snippet and a link to this site. So for example when you make a tutorial and you are using code from this site, you can use one of the following snippet embeddings:

Load Preview Get Code Close Preview







    Share
    Rating
    thumb_up 0 thumb_down
    Author
    Thorolus

    I'm the head developer and co-founder of GlovilGames Studios (glovilgames.com) and other platforms GlovilGames created, like vsnippets.com, worth-calculator.net, python-obfuscator.com and some mobile apps and games.

    Tags
    Android
    View
    Basics



    Informations

    posted on 2016-09-30 18:49:28

    viewed 61 times

    snippet's UUID is 57ee9797ab861

    Similar Snippets