Android Question GradientDrawable with different corner radius?

welu1805

Active Member
Licensed User
Longtime User
In "TabHostExtras1.setTabGradientDrawable2" I can set the corner radius for the GradientDrawable like described below:

"Set a GradientDrawable as the background on all TabIndicators in tabHost1
Corner radii of the GradientDrawable are set individually (in pixels) based upon the number of elements in the array cornerRadius:
1 element defines all corner radii
2 elements define corner radii in order top left and right, bottom left and right
4 elements define corner radii in order top-left, top-right, bottom-right, bottom-left"

How can I set different Corner Radii for a GradientDrawable?
 

klaus

Expert
Licensed User
Longtime User
You can do it with the JavaObject library.
B4X:
Sub setCornerRadii(v As View, Rx_TopLeft As Float, Ry_TopLeft As Float, Rx_TopRight As Float, Ry_TopRight As Float, Rx_BottomRight As Float, Ry_BottomRight As Float, Rx_BottomLeft As Float, Ry_BottomLeft As Float)
    Dim jo As JavaObject = v.Background
    If v.Background Is ColorDrawable OR v.Background Is GradientDrawable Then
        jo.RunMethod("setCornerRadii", Array As Object(Array As Float(Rx_TopLeft, Ry_TopLeft, Rx_TopRight, Ry_TopRight, Rx_BottomRight, Ry_BottomRight, Rx_BottomLeft, Ry_BottomLeft)))
    End If
End Sub
It's possible for ColorDrawable and GradientDrawable backgrounds.
8 parameters are needed, two for each corner.
In the routine above you submit the view to change and the routine checks if the backgrounds ColorDrawables or GradientDrawables.

Note: If the view is added in the code you need to define the background with:
B4X:
Private cdw As ColorDrawable
cdw.Initialize(Colors.White, 0)
Label1.Background = cdw
If you set the background color with:
B4X:
Label1.Color = Colors.White
It throws an error.
 
Last edited:
Upvote 0

welu1805

Active Member
Licensed User
Longtime User
You can do it with the JavaObject library.
B4X:
Sub setCornerRadii(v As View, Rx_TopLeft As Float, Ry_TopLeft As Float, Rx_TopRight As Float, Ry_TopRight As Float, Rx_BottomRight As Float, Ry_BottomRight As Float, Rx_BottomLeft As Float, Ry_BottomLeft As Float)
    Dim jo As JavaObject = v.Background
    If v.Background Is ColorDrawable OR v.Background Is GradientDrawable Then
        jo.RunMethod("setCornerRadii", Array As Object(Array As Float(Rx_TopLeft, Ry_TopLeft, Rx_TopRight, Ry_TopRight, Rx_BottomRight, Ry_BottomRight, Rx_BottomLeft, Ry_BottomLeft)))
    End If
End Sub
It's possible for ColorDrawable and GradientDrawable backgrounds.
8 parameters are needed, two for each corner.
In the routine above you submit the view to change and the routine checks if the backgrounds ColorDrawables or GradientDrawables.
It works good.
Thank you, Klaus.
 
Upvote 0
Top