Dynamically change the background of LinearLayout

How to dynamically change the background of LinearLayout?

+7
android
source share
2 answers

Have you tried one of them:

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

and if it is not updated by itself, this should give it a boost:

  yourLayout.invalidate(); 
+15
source share

I am working now, so I cannot verify this, but I believe that 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); 
+11
source share

All Articles