Change drawable color programmatically
Try this:
Drawable unwrappedDrawable = AppCompatResources.getDrawable(context, R.drawable.my_drawable);
Drawable wrappedDrawable = DrawableCompat.wrap(unwrappedDrawable);
DrawableCompat.setTint(wrappedDrawable, Color.RED);
Using DrawableCompat
is important because it provides backwards compatibility and bug fixes on API 22 devices and earlier.
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);
changing shape color programmatically in android
Change the code as below
GradientDrawable bgShape = (GradientDrawable)btn_ColorPick.getBackground();
bgShape.mutate()
bgShape.setColor(Color.RED);
How to change shape color dynamically?
You could modify it simply like this
GradientDrawable bgShape = (GradientDrawable)btn.getBackground();
bgShape.setColor(Color.BLACK);
How to set a views shape and background color programatically?
You can't change the background color of your drawable shape in xml.
For dynamically changing the color of your shape you have to create custom views.
I came up with these two approaches :
Approach Number 1 :
Creating Custom View using Canvas And Paint.
Here is an useful example of how to create Circle shape programmatically and assign color to it.
Creating CustomView using Canvas And Paint
you can also draw Text when drawing your shape on onDraw()
method.
Approach Number 2 :
Creating Custom View by extending The View
or other ViewGroup
Classes.
Here is a simple sample of the way you should doing it.
public class ColorOptionsView extends View {
private View mValue;
private ImageView mImage;
public ColorOptionsView(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray a = context.obtainStyledAttributes(attrs,
R.styleable.Options, 0, 0);
String titleText = a.getString(R.styleable.Options_titleText);
int valueColor = a.getColor(R.styleable.Options_valueColor,
android.R.color.holo_blue_light);
a.recycle();
// more stuff
}
}
Related Topics
How to Hide Soft Keyboard on Android After Clicking Outside Edittext
How to Return a Result (Startactivityforresult) from a Tabhost Activity
Android Error: Failed to Install *.Apk on Device *: Timeout
How to Get the Absolute Coordinates of a View
How to Send String from One Activity to Another
Android Background Music Service
How to Make Ellipsize="Marquee" Always Scroll
Android Update Activity UI from Service
How to Get Resource Name from Resource Id
Notification Icon with the New Firebase Cloud Messaging System
Using Intents to Pass Data Between Activities
How to Fire Onlistitemclick in Listactivity with Buttons in List
Material Effect on Button with Background Color
How to Run a Method Every X Seconds
Set Transparent Background of an Imageview on Android