Change Shape Solid Color at Runtime Inside Drawable Xml Used as Background

Change shape solid color at runtime inside Drawable xml used as background

Found by me:

    View v = findViewById(R.id.layout_id);

LayerDrawable bgDrawable = (LayerDrawable)v.getBackground();
final GradientDrawable shape = (GradientDrawable) bgDrawable.findDrawableByLayerId(R.id.shape_id);
shape.setColor(----);

Android: Change Shape Color in runtime

See if something similar to this works for you:

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

In my case I have a TextView which has a ShapeDrawable as a background. I wanted to change its color and managed to make this work. Inexplicably, tv2.getBackground() returns a GradientDrawable instead of a ShapeDrawable -- this has been reported elsewhere as well.

Edit: About the color, try setting an alpha value of 0xff. If you notice, even in my code above the setColor() function takes an extra hex value apart from the regular RGB hex value. This is for Alpha/Opacity. If this is set to 0x00 the Drawable will have a black color, irrespective of the RGB (assuming that your background color is black). 0x00 is a completely transparent object & 0xff is a completely opaque object.

How can I change shape color programmatically?

this one work for me and gave me round shape with color

 ShapeDrawable biggerCircle= new ShapeDrawable( new OvalShape());
biggerCircle.setIntrinsicHeight( 60 );
biggerCircle.setIntrinsicWidth( 60);
biggerCircle.setBounds(new Rect(30, 30, 30, 30));
biggerCircle.getPaint().setColor(Color.parseColor(first));//you can give any color here
holder.firstcolor.setBackgroundDrawable(biggerCircle);

Android - Drawable XML containing image on left and solid color to fill width

Try the below code snippet. I hope it will help you.

  <?xml version="1.0"
encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<layer-list>
<item>
<shape>
<gradient android:angle="90"
android:endColor="#ffffff"
android:startColor="#ffffff"
android:type="linear" />

<stroke android:width="1dp"
android:color="#dddbda" />

<corners android:radius="5dp" />

<padding android:bottom="10dp"
android:left="3dp"
android:right="10dp"
android:top="3dp" />
</shape>
</item>
<item>
<bitmap android:gravity="bottom|left"
android:src="@drawable/drdw" />
</item>
</layer-list>
</item>
</selector>

How to change background/color of the drawable set as background of a view?

GradientDrawable background = (GradientDrawable) textView.getBackground();
background.setColor(getResources().getColor(R.color.some_color));

I have considered that you have applied your shape drawable to textView as background.

So you need to use your view in which you have set your background to get background.



Related Topics



Leave a reply



Submit