Android Question Corner Radius for Button

Nitin Joshi

Active Member
Licensed User
My project includes dynamic buttons. For gradient effect, I changed GradientDrawable to GradientMaker because B4i does not support GradientDrawable.

I have to apply rounded corner to buttons. Is there any solution?

I have attached 2 pictures to show the appearance of GradientDrawable and GradientMaker.
 

Attachments

  • GradientDrawable.jpeg
    GradientDrawable.jpeg
    46.7 KB · Views: 68
  • GradientMaker.jpeg
    GradientMaker.jpeg
    46.5 KB · Views: 65
Solution
B4X:
Gradient.SetGradient(Button1, "LEFT_RIGHT", Array(xui.Color_Blue, xui.Color_Green))
Button1.SetColorAndBorder(xui.Color_Transparent, 2dip, xui.Color_Red, 10dip)

1739772259044.png

Brian Dean

Well-Known Member
Licensed User
Longtime User
I don't know what you mean by "dynamic buttons", and I don't know about B4i, but in B4A you can set corner radius for buttons in the Designer. The corner radius persists if you resize the button in application code - not sure if that counts as being "dynamic".
 
Upvote 0

teddybear

Well-Known Member
Licensed User
My project includes dynamic buttons. For gradient effect, I changed GradientDrawable to GradientMaker because B4i does not support GradientDrawable.

I have to apply rounded corner to buttons. Is there any solution?

I have attached 2 pictures to show the appearance of GradientDrawable and GradientMaker.
You can do that with conditional compilation .
B4X:
#if B4A
    Btn.Background.As(GradientDrawable).CornerRadius=5dip
#End If
 
Upvote 0

Nitin Joshi

Active Member
Licensed User
I don't know what you mean by "dynamic buttons", and I don't know about B4i, but in B4A you can set corner radius for buttons in the Designer. The corner radius persists if you resize the button in application code - not sure if that counts as being "dynamic".
Dynamically means buttons are added on page during runtime or buttons are not added in designer.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Dynamically means buttons are added on page during runtime or buttons are not added in designer.
This is a big mistake to build the layout programmatically. Especially if you want to build a cross platform solution.
And make sure to handle the page resize event and reorganize the views.

B4XView.SetColorAndBorder allows you to set round corners and it is cross platform. In B4A, it will replace the gradient so you shouldn't use it. I think that it will keep the gradient with B4i. Try it.
 
Upvote 0

Nitin Joshi

Active Member
Licensed User
Dear Erel, thank you for your suggestion....
This is a big mistake to build the layout programmatically.
In my project, at any instance, number of buttons are random so i have to create buttons programmatically. I have created sub to organize evenly over the screen and its working fine.

B4XView.SetColorAndBorder allows you to set round corners and it is cross platform.
I have checked this for the cross platform. This allows rounded corner, however my conclusion is gradient can not be applied. Is there any way to apply gradient effect plus rounded corner for cross platform?

If needed, i can upload sample program from my project.
 
Upvote 0

Nitin Joshi

Active Member
Licensed User
Thanks Erel, well noted..

#If B4i 'Use B4XView.SetColorAndBorder #Else If B4A 'Use GradientDrawable to set corners #End If
Is there any way to apply gradient plus rounded corner because B4XView. SetColorAndBorder can apply one color (gradient not supported)
 
Upvote 0

Nitin Joshi

Active Member
Licensed User
Gradient.SetGradient(Button1, "LEFT_RIGHT", Array(xui.Color_Blue, xui.Color_Green)) Button1.SetColorAndBorder(xui.Color_Transparent, 2dip, xui.Color_Red, 10dip)
Dear Erel, I have tried this code in B4A to check it works or not (because i have not started to debug B4i). Background of Button is Transparent (gradient not applied).
 
Upvote 0

teddybear

Well-Known Member
Licensed User
B4X:
Gradient.SetGradient(Button1, "LEFT_RIGHT", Array(xui.Color_Blue, xui.Color_Green))
Button1.SetColorAndBorder(xui.Color_Transparent, 2dip, xui.Color_Red, 10dip)

View attachment 161789
This code is only for b4i as Erel said at post #8.
B4X:
#If B4i
'Use B4XView.SetColorAndBorder
Gradient.SetGradient(Button1, "LEFT_RIGHT", Array(xui.Color_Blue, xui.Color_Green))
Button1.SetColorAndBorder(xui.Color_Transparent, 2dip, xui.Color_Red, 10dip)
#Else If B4A
'Use GradientDrawable to set corners
 Button1.Background.As(GradientDrawable).CornerRadius=10dip
#End If
B4a you should use the code I posted #3
 
Upvote 0

teddybear

Well-Known Member
Licensed User
It means, code given by Erel will work on B4i? Actually, i have not started to test B4i on my phone. I have recently bought B4i license, yet to bind with my Apple Developer account.
Post #8 just is the solution, post#10 is for B4i
 
Upvote 0

Nitin Joshi

Active Member
Licensed User
Post #8 just is the solution
Understood but Sorry, I am not still clear. i have two questions...
Question 1) In B4A, i have to declare Button as View and for B4i...Button as B4XView, is my understanding correct? like below...Button.Background is possible if Button is declared as View.

Code..:
Dim cd As GradientMaker
#if B4A
    Dim Button As View
    cd.Initialize
    cd.SetGradient(Button, "LEFT_RIGHT",Array(xui.Color_Blue, xui.Color_Green))
    Button.Background.As(GradientDrawable).CornerRadius=10dip
#Else If B4i
    Dim Button As B4XView
    cd.Initialize
    cd.SetGradient(Button, "LEFT_RIGHT",Array(xui.Color_Blue, xui.Color_Green))
    Button.SetColorAndBorder(xui.Color_Transparent, 2dip, xui.Color_Red, 10dip
#End If

Question 2) As i mentioned before, i have not activated Apple Developer Account, Just for the testing purpose, I applied Erel code (SetGradient + SetColorAndBorder) in B4A but it did not get gradient + rounded corner effect, will it support on iOS?
 
Upvote 0
Top