Android Code Snippet [B4X] [XUI] Elevation shadow

Erel

B4X founder
Staff member
Licensed User
The behavior of B4i shadow, B4J shadow and B4A elevation, which are all based on the native features, are quite different than each other.
You can use this code to create the shadow:
B4X:
Public Sub SetShadow (View As B4XView, Offset As Double, Color As Int)
    #if B4J
    Dim DropShadow As JavaObject
    'You might prefer to ignore panels as the shadow is different.
    'If View Is Pane Then Return
    DropShadow.InitializeNewInstance(IIf(View Is Pane, "javafx.scene.effect.InnerShadow", "javafx.scene.effect.DropShadow"), Null)
    DropShadow.RunMethod("setOffsetX", Array(Offset))
    DropShadow.RunMethod("setOffsetY", Array(Offset))
    DropShadow.RunMethod("setRadius", Array(Offset))
    Dim fx As JFX
    DropShadow.RunMethod("setColor", Array(fx.Colors.From32Bit(Color)))
    View.As(JavaObject).RunMethod("setEffect", Array(DropShadow))
    #Else If B4A
    Offset = Offset * 2
    View.As(JavaObject).RunMethod("setElevation", Array(Offset.As(Float)))
    #Else If B4i
    View.As(View).SetShadow(Color, Offset, Offset, 0.5, False)
    #End If
End Sub

Example:
B4X:
SetShadow(Pane1, 4dip, 0xFF757575)
SetShadow(Button1, 4dip, 0xFF757575)



B4i:

1636270843985.png


B4A:

1636270886495.png


B4J:

1636270915591.png
 

Star-Dust

Expert
Licensed User
What's the best (cross-platform) way to add elevation/shadows on XUI views?
Try NativeShadow class
 
Top