How to Change Shape Color Dynamically

How to change shape color dynamically?

You could modify it simply like this

GradientDrawable bgShape = (GradientDrawable)btn.getBackground();
bgShape.setColor(Color.BLACK);

changing shape color programmatically in android

Change the code as below

GradientDrawable bgShape = (GradientDrawable)btn_ColorPick.getBackground();
bgShape.mutate()
bgShape.setColor(Color.RED);

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

How do I dynamically change the color of a drawable shape ? (Android)

try below snippet, give it a try

Have a look at this screenshot

GradientDrawable gd = new GradientDrawable(GradientDrawable.Orientation.TOP_BOTTOM, new int[]{ContextCompat.getColor(this,R.color.white), ContextCompat.getColor(this,R.color.red_500), ContextCompat.getColor(this,R.color.blue_500)});
gd.setShape(GradientDrawable.RECTANGLE);
gd.setStroke(1, ContextCompat.getColor(this, R.color.black));
gd.setCornerRadius(5f);
gd.setBounds(2, 2, 2, 2);
findViewById(R.id.btn_analytics).setBackground(gd);

Changing the color of ShapeDrawable dynamically

If someone is looking for an answer to the same question, here is how I've solved it. I think that it is not possible to change something that has been already drawn on Canvas. So I've been forced to create a custom view inherited from ImageView which has a bitmap as background and I'm saving every point that has been touched to the List and I've overridden onDraw(Canvas canvas) where I redraw every point in List using specific Paint.

How to change drawable color and shape dynamically?

You could remove the first Relative Layout and modify this line eventView.setBackgroundColor(color); simply like this:

((GradientDrawable)eventView.getBackground()).setColor(color);


Related Topics



Leave a reply



Submit