Change background of LinearLayout in Android

AndroidXmlAndroid Linearlayout

Android Problem Overview


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?

Android Solutions


Solution 1 - Android

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

Solution 2 - Android

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

Solution 3 - Android

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

Solution 4 - Android

 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

Solution 5 - Android

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

Solution 6 - Android

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

Solution 7 - Android

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDurgaView Question on Stackoverflow
Solution 1 - AndroidSwathi EPView Answer on Stackoverflow
Solution 2 - AndroidrajView Answer on Stackoverflow
Solution 3 - AndroidAyudhView Answer on Stackoverflow
Solution 4 - AndroidAnuView Answer on Stackoverflow
Solution 5 - AndroidhzrbasaranView Answer on Stackoverflow
Solution 6 - AndroidKyaw HtutView Answer on Stackoverflow
Solution 7 - AndroidSlowLearnahView Answer on Stackoverflow