59

I am working on an Android application. I want to change the background of a LinearLayout element.

What attribute can I set in order to change its background?

beckah
  • 1,543
  • 6
  • 28
  • 61
Durga
  • 1,191
  • 1
  • 12
  • 18

7 Answers7

126

If you want to set through xml using android's default color codes, then you need to do as below:

android:background="@android:color/white"

If you have colors specified in your project's colors.xml, then use:

android:background="@color/white"

If you want to do programmatically, then do:

linearlayout.setBackgroundColor(Color.WHITE);
Keith
  • 811
  • 6
  • 18
Swathi EP
  • 3,864
  • 6
  • 26
  • 25
  • @swathi:the answer you have insisted is valid for application level only but not in framework.Actually i want to change the open source code of android such that the background of linear layout changes that means whenever we are accessing linear layout in application level then linear layout with my specified background should be displayed – Durga Apr 07 '11 at 06:38
  • @Narayana you will have to modify LinearLayout.java in the android source which will be under folder source/frameworks/base/core/java/android/widget – Swathi EP Apr 07 '11 at 06:50
  • @swathi:i am working on that file only. but couldnt get how to resolve my problem. can u plz help me in this?? – Durga Apr 07 '11 at 06:59
  • @Narayana: In LinearLayout constructor try setting background through setBackgroundColor().....I am not sure about it just give an try this is just my assumption, it may or may not work. – Swathi EP Apr 07 '11 at 07:49
  • @Narayana oh cool...can you tell me how did u run the source? – Swathi EP Apr 07 '11 at 13:13
  • @SwathiEp how to change text color inside linear layout? I have set linearlayout background color blue and now want to set text color white. – Muhammad Shahzad Oct 24 '15 at 08:24
  • The xml listed in this answer does not work for me. Android Studio complains "Unknown resource type colors". I'm using "@colors/red". "@colors/****" works fine in other places in the same xml file, but not for android:background. Just fyi for others. – Alyoshak Sep 16 '16 at 15:59
27
LinearLayout li=(LinearLayout)findViewById(R.id.layoutid);

setting the background color fro ur layout.

li.setBackgroundColor(Color.parseColor("#ffff00"));

this is to set the image which u can store in drawable folder

li.setBackgroundDrawable(drwableItem);

some resource for display purpose animation or img

li.setBackgroundResource(R.id.bckResource);
Someone Somewhere
  • 23,475
  • 11
  • 118
  • 166
raj
  • 743
  • 6
  • 21
9

u just used attribute

  • android:background="#ColorCode" for colors

    if your image save in drawable folder then used :-

  • android:background="@drawable/ImageName" for image setting

Ayudh
  • 710
  • 5
  • 9
  • @Aydudh:This works fine in Application level but not in Frameworks.i want to make changes in the open source code of android – Durga Apr 07 '11 at 06:32
3

1- Select LinearLayout findViewById

LinearLayout llayout =(LinearLayout) findViewById(R.id.llayoutId); 

2- Set color from R.color.colorId

llayout.setBackgroundColor(getResources().getColor(R.color.colorId));
hzrbasaran
  • 31
  • 3
2
 android:background="@drawable/ic_launcher"

should be included inside Layout tab. where ic_launcher is image name that u can put inside project folder/res/drawable . you can copy any number of images and make it as background

Vikas S Singh
  • 1,748
  • 1
  • 14
  • 29
Anu
  • 21
  • 1
1

Use this code, where li is the LinearLayout: li.setBackgroundColor(Color.parseColor("#ffff00"));

Niki van Stein
  • 10,564
  • 3
  • 29
  • 62
Kyaw Htut
  • 11
  • 2
0

If your using a background resource and wish to change the resource out you can use setBackgroundResource() function.

ui_item.setBackgroundResource(R.drawable.myResource)

A background resource in XML would look like:

<LinearLayout
                android:id="@+id/ui_item"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@drawable/background_01"
                android:orientation="vertical">