Android Question [Solved] Button corner radius reset to 0 when change color

aeric

Expert
Licensed User
Longtime User
I tried to make one or another button change its colour using code. When the button click is fired, the corner radius is lost. How can I make the buttons not losing their rounded corners?

Screenshot_1550494561.png Screenshot_1550494245.png Screenshot_1550494250.png

B4X:
#Region  Project Attributes
    #ApplicationLabel: Buttons
    #VersionCode: 1
    #VersionName:
    #SupportedOrientations: portrait
    #CanInstallToExternalStorage: False
#End Region

#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: True
#End Region

Sub Process_Globals

End Sub

Sub Globals
    Private btnOff As Button
    Private btnOn As Button
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("1")
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub btnOn_Click
    btnOn.Color = Colors.RGB(0, 255, 0)
    btnOff.Color = Colors.RGB(105, 105, 105)
End Sub

Sub btnOff_Click
    btnOn.Color = Colors.RGB(105, 105, 105)
    btnOff.Color = Colors.RGB(255, 20, 147)
End Sub
 

Attachments

  • Rounded.zip
    4.6 KB · Views: 131

aeric

Expert
Licensed User
Longtime User
Thanks @asales
The code works. I forgotten this method. :)
B4X:
Sub btnOn_Click
    Dim cd1 As ColorDrawable
    Dim cd2 As ColorDrawable
    cd1.Initialize(Colors.RGB(0, 255, 0), 50dip)
    cd2.Initialize(Colors.RGB(105, 105, 105), 50dip)
    btnOn.Background = cd1
    btnOff.Background = cd2
End Sub

Sub btnOff_Click
    Dim cd1 As ColorDrawable
    Dim cd2 As ColorDrawable
    cd1.Initialize(Colors.RGB(105, 105, 105), 50dip)
    cd2.Initialize(Colors.RGB(255, 20, 147), 50dip)
    btnOn.Background = cd1
    btnOff.Background = cd2
End Sub
 
Upvote 0

aeric

Expert
Licensed User
Longtime User
1. It is simplest to change the colors with the designer
2. If you do it programmatically then it is better to use StateListDrawable with ColorDrawables.
I have used the StateListDrawable in designer but when I change the colour with code, the corner reset to 0.
The StateListDrawable only have Enabled, Disabled and Pressed Drawable. I want the button change colour.

1.png
 
Upvote 0

aeric

Expert
Licensed User
Longtime User
I found there is a typo error in example code for StateListDrawable.

typo.png
 
Upvote 0
Top