HyperX
HyperX

Reputation: 39

Set shape color programmatically

I am using keyboard view where i have custom xml shape thath i can set specific buttons a color here is my shape xml:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"> 
  <solid android:color="#80B3B3B3"/> 
  <corners android:bottomRightRadius="2dp" android:bottomLeftRadius="2dp" 
     android:topLeftRadius="2dp" android:topRightRadius="2dp"/> 
</shape>

Then I overwrite my onDraw method for Keyboard View:

@Override
    public void onDraw(Canvas canvas)
    {
        super.onDraw(canvas);
        List<Key> keys = getKeyboard().getKeys();
        for (Key key : keys) {            
            Drawable dr = (Drawable)this.getResources().getDrawable(R.drawable.keyshape);               
            dr.setBounds(key.x, key.y, key.x + key.width, key.y + key.height);
            dr.draw(canvas);
        }
    }

Now I want to allow that user set this color of Shape any idea how can I set this before drawing?

Upvotes: 1

Views: 1293

Answers (3)

Chk0nDanger
Chk0nDanger

Reputation: 1231

If you want change only the colors on the shape from the xml drawable, I changed your onDraw a little to do it. It will be a GradientDrawable if you define it in xml with <shape> tag.

        Drawable dr = this.getResources().getDrawable(R.drawable.keyshape);
        if (dr instanceof GradientDrawable) {
            GradientDrawable gradientDr = (GradientDrawable) dr;
            gradientDr.setBounds(key.x, key.y, key.x + key.width, key.y + key.height);
            gradientDr.setColor(Color.GREEN);
            gradientDr.draw(canvas);
        }

Upvotes: 0

canova
canova

Reputation: 3975

First of all, you should do a custom view that you want to set and extend it (TextView or Button w/e I will write sample as extending View) since you are not able to edit shape xml programmatically, you will create it as a whole.

Override needed behaviours and constructors for your View. Now you will create a method setGradientColors that take two colours as int values (bottom and top gradient colors). You can modify it for setting radius for rounded corners as well.

public class GradientView extends View{

    public GradientView(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
    }

    public GradientView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        // TODO Auto-generated constructor stub
    }
    public GradientView(Context context, AttributeSet attrs) {
        super(context, attrs);
        // TODO Auto-generated constructor stub
    }

    public void setGradientColors(int bottomColor, int topColor) {
        GradientDrawable gradient = new GradientDrawable(Orientation.BOTTOM_TOP, new int[] {bottomColor, topColor});
        gradient.setShape(GradientDrawable.RECTANGLE);
        gradient.setCornerRadius(10.f);
        this.setBackgroundDrawable(gradient);
    }

}

You are going to use this new view and set shape programmatically as:

GradientView view = (GradientView) findViewById(R.id.customView);
// bottom color - top color order
view.setGradientColors(Color.parseColor("#ff0000"), Color.parseColor("#0000ff"));

If you need further assistance and full explanation about basics, I have achieved this in my project by following this Link

Hope it is going to help you as well.

Upvotes: 1

Android Priya
Android Priya

Reputation: 676

You can't change shape colour pro grammatically either you have to create new shape with different colour or you can use :

imageView.getBackground().setColorFilter(Color.parseColor("#BBBDBF"), Mode.MULTIPLY);
imageview.invalidate();

it will multiply the given colour code with your image background and will show effect of colour changed.

Upvotes: 0

Related Questions