I have created a ShapeDrawable and placed it within an imageview that is dynamically created within my layout. Here’s the code I use to assign the drawable to my ImageView. Notice that I call on a method to set a random color, I also set the height and width using user filled text fields, but that’s irrelevant here.
ShapeDrawable shape;
public void makeShape(){
shape = new ShapeDrawable(new RectShape());
shape.setIntrinsicHeight(Integer.parseInt(ET_Height.getText().toString()));
shape.setIntrinsicWidth(Integer.parseInt(ET_Width.getText().toString()));
shape.getPaint().setColor(chooseColor());
iView.setImageDrawable(shape);
}
This part of the code works wonderfully, it’s when I later try and access the Drawable to change it’s color on a user click, utilizing the same color method from before.
public void onClick(View v){
iView.getDrawable().getPaint().setColor(chooseColor());
}
Obviously this code doesn’t work, but it’s the closest I have come to being able to complete it. I can’t access the drawable via “shape” either, and I don’t understand why.
So what’s the best way to gain access to the drawable to change it’s color, alpha, rotation, etc in the onClick method?
Thanks!
Try iView.setImageDrawable(shape) again after changing the shape parameters.