TextView tv2 = (TextView) rl.findViewById(R.id.toggle_indicator); /* Refer to http://developer.android.com/reference/android/graphics/drawable/GradientDrawable.html#mutate() to understand why we need to mutate the GradientDrawable*/ GradientDrawable sd = (GradientDrawable) tv2.getBackground().mutate(); sd.setColor(0xff999999); sd.invalidateSelf();
public static void setCustomizerDrawableColor(final View target, final int color) { final Drawable d = target.getDrawable(); LayerDrawable layer = (LayerDrawable)d; GradientDrawable gradient = (GradientDrawable)layer.getDrawable(0); gradient.setColor(color); gradient.invalidateSelf(); layer.invalidateSelf(); target.invalidate(); }
tv_img_tag = (Button) vi.findViewById(R.id.tv_img_tag); tv_img_tag.setBackgroundResource(R.drawable.roundedtexts); GradientDrawable sd = (GradientDrawable) tv_img_tag.getBackground().mutate(); sd.setColor(0xff999999); sd.invalidateSelf();
GradientDrawable sd = (GradientDrawable) (view.getCompoundDrawables())[0]; sd.setColor(Color.red); sd.invalidateSelf();
View viewrectangle; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); viewrectangle = (View) findViewById(R.id.myRectangleView); } public void doClick(View v) { GradientDrawable sd = (GradientDrawable) viewrectangle.getBackground(); sd.setColor(0xffffff00); sd.invalidateSelf(); }
Button button = (Button) buttonView; GradientDrawable gradientDrawable = (GradientDrawable) button.getBackground(); gradientDrawable.mutate(); // needed line gradientDrawable.setColor(Color.RED); gradientDrawable.invalidateSelf();
LayerDrawable layer = (LayerDrawable)(view.getBackground()); int randomColor = Color.rgb(redRandom.nextInt(255),greenRandom.nextInt(255),blueRandom.nextInt(255)); GradientDrawable sd = (GradientDrawable) (layer.findDrawableByLayerId(R.id.tab_package_shape)); sd.setStroke(0, randomColor); sd.setColor(randomColor); sd.invalidateSelf();
public class MyActivity extends Activity { private ImageView imageView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); imageView = (ImageView) findViewById(R.id.imageView); imageView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { GradientDrawable sd = (GradientDrawable) imageView.getBackground().mutate(); sd.setColor(Color.parseColor("#CCC111")); sd.invalidateSelf(); } }); } }
final GradientDrawable gradientDrawable = (GradientDrawable) view.getBackground().mutate(); gradientDrawable.setColor(color); gradientDrawable.invalidateSelf();