Change Shape Drawable Solid Color?

Problems with Canvas, OpenGL, etc...

Change Shape Drawable Solid Color?

Postby mast3rpyr0 » Mon Aug 09, 2010 1:06 pm

Is it possible to change the color of this Shape drawable defined in XML back in the java code?
Code: Select all
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <corners
        android:radius="10dp" />
    <solid
        android:color="#FFFFFF" />
</shape>


Code: Select all
ImageView iv = (ImageView) findViewById(R.id.ColorBox);
       Drawable d = iv.getBackground();


Thanks for the help
mast3rpyr0
Freshman
Freshman
 
Posts: 5
Joined: Wed Jun 23, 2010 7:16 am

Top

Re: Change Shape Drawable Solid Color?

Postby MichaelEGR » Mon Aug 09, 2010 1:39 pm

A quick reply without any testing, but perhaps you can cast "d" to a ShapeDrawable and get stored Paint reference and change the color via the Paint reference. Something in that direction may likely work.
Founder & Principal Architect; EGR Software LLC
http://www.typhonrt.org
http://www.egrsoftware.com
User avatar
MichaelEGR
Senior Developer
Senior Developer
 
Posts: 147
Joined: Thu Jan 21, 2010 5:30 am
Location: San Francisco, CA

Re: Change Shape Drawable Solid Color?

Postby mast3rpyr0 » Mon Aug 09, 2010 1:58 pm

Doesnt seem like it. I tried this:

Code: Select all
ShapeDrawable sd = (ShapeDrawable) getResources().getDrawable(R.drawable.color_box);
       sd.getPaint().setColor(0x00000000);


and I get a ClassCastException. BUT whats interesting is that that exception looks like this:

Code: Select all
android.graphics.drawable.GradientDrawable


And clearly it is a ShapeDrawable?
mast3rpyr0
Freshman
Freshman
 
Posts: 5
Joined: Wed Jun 23, 2010 7:16 am

Re: Change Shape Drawable Solid Color?

Postby MichaelEGR » Mon Aug 09, 2010 2:26 pm

Then perhaps try casting to GradientDrawable and calling the setColor method. Seems like an implementation detail when using the base shape XML tag in the layout regarding the backing drawable with the default shape tag. Perhaps if you want to specify the precise drawable instead of <shape ..... /> Use the specific drawable class in the XML layout. Again off the top of my head, so you'll need to do a little investigation and testing.
Founder & Principal Architect; EGR Software LLC
http://www.typhonrt.org
http://www.egrsoftware.com
User avatar
MichaelEGR
Senior Developer
Senior Developer
 
Posts: 147
Joined: Thu Jan 21, 2010 5:30 am
Location: San Francisco, CA

Re: Change Shape Drawable Solid Color?

Postby mast3rpyr0 » Mon Aug 09, 2010 2:37 pm

Odd, that works great haha. Thanks.

Now i need to have the object redrawn after the change, i dont have a Canvas object to use the Draw method.. Do i need one or is there another way?
mast3rpyr0
Freshman
Freshman
 
Posts: 5
Joined: Wed Jun 23, 2010 7:16 am

Re: Change Shape Drawable Solid Color?

Postby MichaelEGR » Mon Aug 09, 2010 2:53 pm

Call postInvalidate() on the View in question. In this case "ImageView iv" in your sample code above.
Founder & Principal Architect; EGR Software LLC
http://www.typhonrt.org
http://www.egrsoftware.com
User avatar
MichaelEGR
Senior Developer
Senior Developer
 
Posts: 147
Joined: Thu Jan 21, 2010 5:30 am
Location: San Francisco, CA

Top

Re: Change Shape Drawable Solid Color?

Postby ringu » Fri Oct 22, 2010 2:01 pm

Odd, that works great haha. Thanks.


Could you please put in the code, I have god the same problem and can't get rid of it.



change: finally found the solution by myself:
Code: Select all
      GradientDrawable sd = (GradientDrawable) getResources().getDrawable(R.drawable.listheader);
          sd.setColor(0xFFFF00FF);

ringu
Freshman
Freshman
 
Posts: 4
Joined: Sun Jun 20, 2010 10:55 am

Re: Change Shape Drawable Solid Color?

Postby DKDiveDude » Thu Jan 20, 2011 1:11 am

MichaelEGR wrote:Call postInvalidate() on the View in question. In this case "ImageView iv" in your sample code above.


Ok I am using the solution to change color of XML drawable via code and using iv.postInvalidate(), problem is the color is NOT updating.

Now I should mention I am trying to change a color of a drawable placed above a seekbar and inside the seekbar's onProgressChanged.

Any ideas why it's not changing?
DKDiveDude
Junior Developer
Junior Developer
 
Posts: 18
Joined: Mon Dec 13, 2010 2:40 pm

Top

Return to Android 2D/3D Graphics - OpenGL Problems

Who is online

Users browsing this forum: No registered users and 8 guests