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
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
How to Properly Highlight Selected Item on Recyclerview
Passing Arraylist Through Intent
Android How to Work with Asynctasks Progressdialog
How to Access Call Log for Android
Android Emulator Doesn't Take Keyboard Input - Sdk Tools Rev 20
Requestfeature() Must Be Called Before Adding Content
Open Link of Google Play Store in Mobile Version Android
How to Get the Height and Width of the Android Navigation Bar Programmatically
Changing Screen Brightness Programmatically (As with the Power Widget)
How to Change the Text Color of Menu Item in Android
Firebase Analytics Custom Events Params
How to Decompile an Android .Apk File
How to Add a Textview to a Linearlayout Dynamically in Android