Skip to content

Commit 195221e

Browse files
[Android] Fix border styles for touchable components with transparent background color (#3096)
## Description This PR addresses an issue where border styles are not visible on touchable components when the `backgroundColor` is set to transparent in Android. Fixes #3088 ## Test plan Tested on the example app and on the code from the issue.
1 parent 60a3901 commit 195221e

File tree

1 file changed

+24
-15
lines changed

1 file changed

+24
-15
lines changed

android/src/main/java/com/swmansion/gesturehandler/react/RNGestureHandlerButtonViewManager.kt

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -286,22 +286,11 @@ class RNGestureHandlerButtonViewManager : ViewGroupManager<ButtonViewGroup>(), R
286286
return false
287287
}
288288

289-
private fun updateBackgroundColor(backgroundColor: Int, selectable: Drawable?) {
289+
private fun updateBackgroundColor(backgroundColor: Int, borderDrawable: Drawable, selectable: Drawable?) {
290290
val colorDrawable = PaintDrawable(backgroundColor)
291-
val borderDrawable = PaintDrawable(Color.TRANSPARENT)
292291

293292
if (hasBorderRadii) {
294293
colorDrawable.setCornerRadii(buildBorderRadii())
295-
borderDrawable.setCornerRadii(buildBorderRadii())
296-
}
297-
298-
if (borderWidth > 0f) {
299-
borderDrawable.paint.apply {
300-
style = Paint.Style.STROKE
301-
strokeWidth = borderWidth
302-
color = borderColor ?: Color.BLACK
303-
pathEffect = buildBorderStyle()
304-
}
305294
}
306295

307296
val layerDrawable = LayerDrawable(if (selectable != null) arrayOf(colorDrawable, selectable, borderDrawable) else arrayOf(colorDrawable, borderDrawable))
@@ -324,6 +313,7 @@ class RNGestureHandlerButtonViewManager : ViewGroupManager<ButtonViewGroup>(), R
324313
}
325314

326315
val selectable = createSelectableDrawable()
316+
val borderDrawable = createBorderDrawable()
327317

328318
if (hasBorderRadii && selectable is RippleDrawable) {
329319
val mask = PaintDrawable(Color.WHITE)
@@ -334,13 +324,32 @@ class RNGestureHandlerButtonViewManager : ViewGroupManager<ButtonViewGroup>(), R
334324
if (useDrawableOnForeground && Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
335325
foreground = selectable
336326
if (_backgroundColor != Color.TRANSPARENT) {
337-
updateBackgroundColor(_backgroundColor, null)
327+
updateBackgroundColor(_backgroundColor, borderDrawable, null)
338328
}
339329
} else if (_backgroundColor == Color.TRANSPARENT && rippleColor == null) {
340-
background = selectable
330+
background = LayerDrawable(arrayOf(selectable, borderDrawable))
341331
} else {
342-
updateBackgroundColor(_backgroundColor, selectable)
332+
updateBackgroundColor(_backgroundColor, borderDrawable, selectable)
333+
}
334+
}
335+
336+
private fun createBorderDrawable(): Drawable {
337+
val borderDrawable = PaintDrawable(Color.TRANSPARENT)
338+
339+
if (hasBorderRadii) {
340+
borderDrawable.setCornerRadii(buildBorderRadii())
341+
}
342+
343+
if (borderWidth > 0f) {
344+
borderDrawable.paint.apply {
345+
style = Paint.Style.STROKE
346+
strokeWidth = borderWidth
347+
color = borderColor ?: Color.BLACK
348+
pathEffect = buildBorderStyle()
349+
}
343350
}
351+
352+
return borderDrawable
344353
}
345354

346355
private fun createSelectableDrawable(): Drawable? {

0 commit comments

Comments
 (0)