Skip to content

Commit bc15f71

Browse files
satya164nicktate
authored andcommitted
Support customizing thumb, track and progress colors for slider on Android
Summary: **Motivation** Ability to customize slider colors is desirable to match the brand. Currently iOS allows using images for slider parts, but android doesn't have any customization options. This PR adds support for customizing `thumbTintColor`, `trackTintColor` and `progressTintColor`. **Test plan (required)** Run UIExplorer example with the changes and verify everything works fine. ![image](https://cloud.githubusercontent.com/assets/1174278/22020752/f32a6eec-dcdf-11e6-928d-481bb28bd0a3.png) cc brentvatne Closes facebook#11946 Differential Revision: D4427474 fbshipit-source-id: ec3a38db600bac6108691a4cfa15e2409143b9f3
1 parent fb48f5b commit bc15f71

File tree

5 files changed

+66
-17
lines changed

5 files changed

+66
-17
lines changed
-223 Bytes
Loading
41.1 KB
Loading

Examples/UIExplorer/js/SliderExample.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -132,16 +132,23 @@ exports.examples = [
132132
},
133133
{
134134
title: 'Custom min/max track tint color',
135-
platform: 'ios',
136135
render(): React.Element<any> {
137136
return (
138137
<SliderExample
139-
minimumTrackTintColor={'red'}
140-
maximumTrackTintColor={'green'}
138+
minimumTrackTintColor={'blue'}
139+
maximumTrackTintColor={'red'}
140+
value={0.5}
141141
/>
142142
);
143143
}
144144
},
145+
{
146+
title: 'Custom thumb color',
147+
platform: 'android',
148+
render(): React.Element<any> {
149+
return <SliderExample thumbTintColor={'blue'} />;
150+
}
151+
},
145152
{
146153
title: 'Custom thumb image',
147154
platform: 'ios',

Libraries/Components/Slider/Slider.js

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
'use strict';
1313

1414
var Image = require('Image');
15+
var ColorPropType = require('ColorPropType');
1516
var NativeMethodsMixin = require('NativeMethodsMixin');
1617
var ReactNativeViewAttributes = require('ReactNativeViewAttributes');
1718
var Platform = require('Platform');
@@ -68,18 +69,16 @@ var Slider = React.createClass({
6869
maximumValue: PropTypes.number,
6970

7071
/**
71-
* The color used for the track to the left of the button. Overrides the
72-
* default blue gradient image.
73-
* @platform ios
72+
* The color used for the track to the left of the button.
73+
* Overrides the default blue gradient image on iOS.
7474
*/
75-
minimumTrackTintColor: PropTypes.string,
75+
minimumTrackTintColor: ColorPropType,
7676

7777
/**
78-
* The color used for the track to the right of the button. Overrides the
79-
* default blue gradient image.
80-
* @platform ios
78+
* The color used for the track to the right of the button.
79+
* Overrides the default blue gradient image on iOS.
8180
*/
82-
maximumTrackTintColor: PropTypes.string,
81+
maximumTrackTintColor: ColorPropType,
8382

8483
/**
8584
* If true the user won't be able to move the slider.
@@ -114,6 +113,12 @@ var Slider = React.createClass({
114113
*/
115114
thumbImage: Image.propTypes.source,
116115

116+
/**
117+
* Color of the foreground switch grip.
118+
* @platform android
119+
*/
120+
thumbTintColor: ColorPropType,
121+
117122
/**
118123
* Callback continuously called while the user is dragging the slider.
119124
*/

ReactAndroid/src/main/java/com/facebook/react/views/slider/ReactSliderManager.java

Lines changed: 43 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,16 @@
99

1010
package com.facebook.react.views.slider;
1111

12-
import java.util.Map;
13-
12+
import android.graphics.Color;
13+
import android.graphics.PorterDuff;
14+
import android.graphics.drawable.Drawable;
15+
import android.graphics.drawable.LayerDrawable;
16+
import android.util.TypedValue;
1417
import android.view.View;
1518
import android.view.ViewGroup;
1619
import android.widget.SeekBar;
1720

18-
import com.facebook.yoga.YogaMeasureMode;
19-
import com.facebook.yoga.YogaMeasureFunction;
20-
import com.facebook.yoga.YogaNodeAPI;
21-
import com.facebook.yoga.YogaMeasureOutput;
21+
import com.facebook.react.R;
2222
import com.facebook.react.bridge.ReactContext;
2323
import com.facebook.react.common.MapBuilder;
2424
import com.facebook.react.uimanager.LayoutShadowNode;
@@ -27,6 +27,12 @@
2727
import com.facebook.react.uimanager.UIManagerModule;
2828
import com.facebook.react.uimanager.ViewProps;
2929
import com.facebook.react.uimanager.annotations.ReactProp;
30+
import com.facebook.yoga.YogaMeasureFunction;
31+
import com.facebook.yoga.YogaMeasureMode;
32+
import com.facebook.yoga.YogaMeasureOutput;
33+
import com.facebook.yoga.YogaNodeAPI;
34+
35+
import java.util.Map;
3036

3137
/**
3238
* Manages instances of {@code ReactSlider}.
@@ -145,6 +151,37 @@ public void setStep(ReactSlider view, double value) {
145151
view.setStep(value);
146152
}
147153

154+
@ReactProp(name = "thumbTintColor", customType = "Color")
155+
public void setThumbTintColor(ReactSlider view, Integer color) {
156+
if (color == null) {
157+
view.getThumb().clearColorFilter();
158+
} else {
159+
view.getThumb().setColorFilter(color, PorterDuff.Mode.SRC_IN);
160+
}
161+
}
162+
163+
@ReactProp(name = "minimumTrackTintColor", customType = "Color")
164+
public void setMinimumTrackTintColor(ReactSlider view, Integer color) {
165+
LayerDrawable drawable = (LayerDrawable) view.getProgressDrawable().getCurrent();
166+
Drawable background = drawable.findDrawableByLayerId(android.R.id.background);
167+
if (color == null) {
168+
background.clearColorFilter();
169+
} else {
170+
background.setColorFilter(color, PorterDuff.Mode.SRC_IN);
171+
}
172+
}
173+
174+
@ReactProp(name = "maximumTrackTintColor", customType = "Color")
175+
public void setMaximumTrackTintColor(ReactSlider view, Integer color) {
176+
LayerDrawable drawable = (LayerDrawable) view.getProgressDrawable().getCurrent();
177+
Drawable progress = drawable.findDrawableByLayerId(android.R.id.progress);
178+
if (color == null) {
179+
progress.clearColorFilter();
180+
} else {
181+
progress.setColorFilter(color, PorterDuff.Mode.SRC_IN);
182+
}
183+
}
184+
148185
@Override
149186
protected void addEventEmitters(final ThemedReactContext reactContext, final ReactSlider view) {
150187
view.setOnSeekBarChangeListener(ON_CHANGE_LISTENER);

0 commit comments

Comments
 (0)