Jaitnium
Jaitnium

Reputation: 650

Is there any way to have the Ripple effect's color be dynamic in Android?

I'm using the ripple effect on clickable elements in my user interface for my app:

<?xml version="1.0" encoding="utf-8"?>
<ripple
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:color="@color/MenuGray">
    <item>
        <shape
            android:shape="rectangle">
        </shape>
    </item>
</ripple>

The issue is that I want there to be a ripple effect on elements that have different background colors. Is there any way to have the ripple color be dynamic so it'll be a shade darker than the element that's being clicked on, or do I need to have multiple ripple.xml files for every element with a different background color?

Upvotes: 1

Views: 832

Answers (1)

Cheticamp
Cheticamp

Reputation: 62841

If you load the RippleDrawable you should be able to set the color.

public void setColor (ColorStateList color)

Sets the ripple color.

Here is an example that changes the RippleDrawable for a couple of TextViews.

enter image description here

MainActivity.java

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        TextView textView = findViewById(R.id.textView);
        setRippleColor(textView, Color.RED);

        textView = findViewById(R.id.textView2);
        setRippleColor(textView, getResources().getColor(android.R.color.holo_blue_light));

    }

    private void setRippleColor(View view, int color) {
        RippleDrawable drawable = (RippleDrawable) view.getBackground();
        ColorStateList stateList = new ColorStateList(
                new int[][]{new int[]{android.R.attr.state_pressed}},
                new int[]{color}
        );
        drawable.setColor(stateList);
        view.setBackground(drawable);
    }
}

activity_main.xml

<android.support.constraint.ConstraintLayout 
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/ripple_drawable"
        android:clickable="true"
        android:focusable="true"
        android:text="Make it red"
        android:textSize="30sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        android:background="@drawable/ripple_drawable"
        android:clickable="true"
        android:focusable="true"
        android:text="Make it blue"
        android:textSize="30sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView"
        app:layout_constraintVertical_bias="0.0" />

</android.support.constraint.ConstraintLayout>

ripple_drawable.xml

<ripple android:color="@android:color/darker_gray">
    <item android:id="@android:id/mask"
        android:drawable="@android:color/white" />
</ripple>

You may also find "Programmatically create a RippleDrawable of any color" helpful.

Upvotes: 2

Related Questions