8

How to dynamically change the background LinearLayout?

3
  • In what way? What are you looking to change? Commented Aug 18, 2010 at 13:54
  • If you change the position of the phone to change its screen horizontally or vertically. Commented Aug 18, 2010 at 14:16
  • You should have specified your situation in your post bizon, that would have helped us to answer you. look at the comment from kcoppock, it should help you. Commented Aug 18, 2010 at 22:33

2 Answers 2

15

Did you try one of these :

yourLayout.setBackgroundColor(int color);
yourLayout.setBackgroundDrawable(Drawable d);
yourLayout.setBackgroundResource(int resid);

and if does not refresh on its own, this should give it a boost :

   yourLayout.invalidate();
Sign up to request clarification or add additional context in comments.

Comments

11

I'm at work right now, so I can't test this, but I believe this should work:

LinearLayout linLay = (LinearLayout) findViewById(R.id.theLinearLayoutId);

//set background to a color
linLay.setBackgroundColor(Color.parseColor("#404040"));

//set background to a drawable
linLay.setBackgroundDrawable(drawableItem);

//set background to a resource
linLay.setBackgroundResource(R.id.backgroundResource);

4 Comments

Bizon, from your comment, are you just trying to change to a different layout when the phone is switched to landscape mode versus portrait mode? If so, you should make a new layout.xml file, and place it under res/layout-land/ developer.android.com/guide/topics/resources/index.html
setBackgroundColor(...) is a integer method. You cannot pass a string a string argument to it.
setBackgroundColor(Color.parseColor("")); would work, just put the color string inside the parseColor method
@user413881, I hit the "Up Arrow" just now because I tried and it neither worked for me but, I looked my code again, and found that works, the issue was I have a customized ChipLayout (extends LinearLayout), and I only changed the background resource on the reference of the ChipLayout, but forgot to change the background resource of the ChipLayout itself, change to operate on the ChipLayout everything works. Hope this could be helpful to other.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.