Skip to content

Commit

Permalink
Android: Fix switch ripple color (#30685)
Browse files Browse the repository at this point in the history
Summary:
fix #22370

Use `RippleDrawable` to change ripple color.
According to the [document](https://developer.android.com/reference/android/graphics/drawable/RippleDrawable?hl=en), `RippleDrawable` is added in API 21, so warped the code in the `if` statement for version checking.

## Changelog

<!-- Help reviewers and the release process by writing your own changelog entry. For an example, see:
https://github.com/facebook/react-native/wiki/Changelog
-->

[Android] [Fixed] - Fix wrong ripple color on Switch component

Pull Request resolved: #30685

Test Plan:
1. Create an empty app with react-native 0.63.4, copy&paste the App.js from issue #22370
2. Apply the code for fixing to `node_modules/react-native/ReactAndroid/src/main/java/com/facebook/react/views/switchview/ReactSwitch.java`
3. Configure the project to let it Build from ReactAndroid
4. Check if ripple color has changed correctly
5. Use different color on each state and check if it is working

Test device: Android emulator, Pixel 4, API 30

## Screenshot

### Before
Ripple is always in default color

https://user-images.githubusercontent.com/48589760/103573532-5b4cec80-4f09-11eb-96d7-f75efa6779d9.mov

### After
Ripple color has changed with thumb color

https://user-images.githubusercontent.com/48589760/103573216-d95cc380-4f08-11eb-98fb-494e28c12a9f.mov

Check different color on each state

https://user-images.githubusercontent.com/48589760/103573227-de217780-4f08-11eb-8992-ede5d1dd89c1.mov

Reviewed By: mdvacca

Differential Revision: D27636802

Pulled By: lunaleaps

fbshipit-source-id: fa23cc8b51c642e5e5d9c73371c8ccef3741fd14
  • Loading branch information
rnike authored and facebook-github-bot committed Apr 12, 2021
1 parent a17e3cf commit 1b06835
Showing 1 changed file with 13 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,11 @@
package com.facebook.react.views.switchview;

import android.content.Context;
import android.content.res.ColorStateList;
import android.graphics.PorterDuff;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.RippleDrawable;
import android.os.Build;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.SwitchCompat;

Expand Down Expand Up @@ -59,6 +62,16 @@ public void setTrackColor(@Nullable Integer color) {

public void setThumbColor(@Nullable Integer color) {
setColor(super.getThumbDrawable(), color);

// Set the ripple color with thumb color if >= LOLLIPOP
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
RippleDrawable ripple = (RippleDrawable) super.getBackground();
ColorStateList customColorState =
new ColorStateList(
new int[][] {new int[] {android.R.attr.state_pressed}}, new int[] {color});

ripple.setColor(customColorState);
}
}

/*package*/ void setOn(boolean on) {
Expand Down

0 comments on commit 1b06835

Please sign in to comment.