Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import android.content.Context;
import android.content.res.ColorStateList;
import android.graphics.PorterDuff;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.RippleDrawable;
import android.os.Build;
Expand Down Expand Up @@ -48,6 +49,18 @@ public void setChecked(boolean checked) {
}
}

@Override
public void setBackgroundColor(int color) {
// Ensure RippleDrawable is preserved for >=21
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
setBackground(
new RippleDrawable(
createRippleDrawableColorStateList(color), new ColorDrawable(color), null));
} else {
super.setBackgroundColor(color);
}
}

void setColor(Drawable drawable, @Nullable Integer color) {
if (color == null) {
drawable.clearColorFilter();
Expand All @@ -63,14 +76,10 @@ 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);
// Set the ripple color if background is instance of RippleDrawable
if (color != null && super.getBackground() instanceof RippleDrawable) {
ColorStateList customColorState = createRippleDrawableColorStateList(color);
((RippleDrawable) super.getBackground()).setColor(customColorState);
}
}

Expand Down Expand Up @@ -113,4 +122,9 @@ private void setTrackColor(boolean checked) {
setTrackColor(currentTrackColor);
}
}

private ColorStateList createRippleDrawableColorStateList(@Nullable Integer color) {
return new ColorStateList(
new int[][] {new int[] {android.R.attr.state_pressed}}, new int[] {color});
}
}
23 changes: 23 additions & 0 deletions packages/rn-tester/js/examples/Switch/SwitchExample.js
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,23 @@ class OnChangeExample extends React.Component<{...}, $FlowFixMeState> {
}
}

class ContainerBackgroundColorStyleExample extends React.Component<
{...},
$FlowFixMeState,
> {
render() {
return (
<View>
<Switch
style={{backgroundColor: 'blue'}}
thumbColor="white"
value={true}
/>
</View>
);
}
}

exports.title = 'Switch';
exports.documentationURL = 'https://reactnative.dev/docs/switch';
exports.category = 'UI';
Expand Down Expand Up @@ -291,6 +308,12 @@ exports.examples = [
return <OnChangeExample />;
},
},
{
title: "The container's background color can be set",
render(): React.Element<any> {
return <ContainerBackgroundColorStyleExample />;
},
},
];

if (Platform.OS === 'ios') {
Expand Down