2

i have the following parallelogram shape :

<vector xmlns:android="http://schemas.android.com/apk/res/android"
    android:width="200dp"
    android:height="50dp"
    android:viewportWidth="200"
    android:viewportHeight="50">

    <path android:fillColor="#000000"
    android:pathData="M 200 0 L 20 0 L 0 50 L 180 50 L 200 0" />
</vector>

and i have used it in this way

    <View

        android:layout_width="66dp"
        android:layout_height="26dp"
        android:layout_centerInParent="true"
        android:layout_marginEnd="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="112dp"
        android:background="@drawable/shape"
        android:onClick="test"
        android:tag="p112"
        app:layout_constraintHorizontal_bias="0.615"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

now i want to change its color from black to white after tapping. function "test" would trigger after touching. but all i have found changes the color of background that would make my black parallelogram to a white rectangle. is there any way i can change its color. even manipulating the xml would be fine. tnx

test function:

    public void test(View view){
        view.setBackgroundColor(Color.WHITE);
    }
3
  • Have you tried using a xml selector drawable for touching? Commented May 22, 2017 at 13:07
  • You need a new drawable that's white, by the way, if you are wanting to only change the shape color Commented May 22, 2017 at 13:09
  • @cricket_007 you mean i create a new one and exactly place it on the touched shape? is there any way i set the color to each view separately? Commented May 22, 2017 at 13:19

3 Answers 3

9

Drawable backgroundDrawable = DrawableCompat.wrap(view.getBackgroundDrawable()).mutate(); DrawableCompat.setTint(backgroundDrawable, color); You can use this!!

Sign up to request clarification or add additional context in comments.

2 Comments

instead of getBackgroundDrawable() I used getBackground() and it worked. thanks a million
but i have another problem...i made my parallelogram transparent with border, calling test function just changers the border color but when i set a color (not transparent) for my shape, test function would change its fill color. what shall i do?
1

You cannot change the color of individual path at runtime. You can however , change the color of entire vector.

1 Comment

i have 64 of this shape and want to change the only one which is touched not all of them. is it possible? cause i think changing the whole vector color would change all my 64 shapes.
0

Try using setBackgroundTint instead of color. that should alter the resource of the background.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.