Change background of LinearLayout in Android
Asked Answered
S

7

60

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?

Seguidilla answered 7/4, 2011 at 6:17 Comment(0)
G
128

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);
Glyph answered 7/4, 2011 at 6:35 Comment(7)
@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 displayedSeguidilla
@Narayana you will have to modify LinearLayout.java in the android source which will be under folder source/frameworks/base/core/java/android/widgetGlyph
@swathi:i am working on that file only. but couldnt get how to resolve my problem. can u plz help me in this??Seguidilla
@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.Glyph
@Narayana oh cool...can you tell me how did u run the source?Glyph
@SwathiEp how to change text color inside linear layout? I have set linearlayout background color blue and now want to set text color white.Benildis
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.Ungrudging
C
28
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);
Cluff answered 7/4, 2011 at 6:37 Comment(3)
If u dnt mind li.setBackgroundColor("#ffff00"); this code is not right li.setBackgroundColor(Color.parseColor("#ffff00")); this correctDistinct
Hemant - I edited the example, see the correct use of setBackgroundColor()Submerge
how did to import "Color" in the class?Noellanoelle
A
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

Avion answered 7/4, 2011 at 6:25 Comment(1)
@Aydudh:This works fine in Application level but not in Frameworks.i want to make changes in the open source code of androidSeguidilla
T
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));
Thamora answered 7/7, 2017 at 12:6 Comment(0)
C
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

Cormorant answered 12/12, 2012 at 4:43 Comment(0)
E
1

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

Encarnacion answered 5/10, 2015 at 7:45 Comment(0)
U
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">
Univocal answered 23/12, 2020 at 18:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.