Android Question Changing buttons textcolor resizes button

kostefar

Active Member
Licensed User
Longtime User
Dear All,

I´m struggling to understand what I´m doing wrong here. The below code loads an activity with a two buttons of various sizes.. When you click the biggest button, it should change the textcolor of all buttons which it does, but it also changes the size of the big button to that of the small one.

Would someone be able to point out what´s going wrong?

Thanks!

B4X:
#Region  Project Attributes
    #ApplicationLabel: B4A Example
    #VersionCode: 1
    #VersionName:

    #SupportedOrientations: unspecified
    #CanInstallToExternalStorage: False
#End Region

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

Sub Process_Globals
 
End Sub

Sub Globals
Dim clrs(2) As Int
Dim newbutton As Button
Dim newbutton2 As Button
End Sub

Sub Activity_Create(FirstTime As Boolean)
    newbutton.Initialize("newbutton")
    newbutton2.Initialize("newbutton2")
    Activity.AddView(newbutton,50%x,50%y,100dip,100dip)
    Activity.AddView(newbutton2,75%x,75%y,50dip,50dip)
    newbutton.Text = "test"
    newbutton2.Text = "test"
    Dim grad As GradientDrawable
    clrs(0) = Colors.Blue
    clrs(1) = Colors.red
    grad.Initialize("LEFT_RIGHT",clrs)
    For Each v As View In Activity.GetAllViewsRecursive
        If v Is Button Then
            Dim b As Button = V
                    b.Background = grad
        End If
    Next
End Sub
Sub Activity_Resume  
End Sub
Sub Activity_Pause (UserClosed As Boolean)
End Sub
Sub newbutton_Click
    For Each v As View In Activity.GetAllViewsRecursive
        If v Is Button Then
            Dim b As Button = V
            b.TextColor = Colors.cyan
            v = b
            End If
Next
End Sub
 

MarcoRome

Expert
Licensed User
Longtime User
B4X:
#Region  Project Attributes
    #ApplicationLabel: B4A Example
    #VersionCode: 1
    #VersionName:

    #SupportedOrientations: unspecified
    #CanInstallToExternalStorage: False
#End Region

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

Sub Process_Globals
End Sub

Sub Globals
    Dim clrs(2) As Int
    Dim newbutton As Button
    Dim newbutton2 As Button
End Sub

Sub Activity_Create(FirstTime As Boolean)
    newbutton.Initialize("newbutton")
    newbutton2.Initialize("newbutton2")
    Activity.AddView(newbutton,50%x,50%y,100dip,100dip)
    Activity.AddView(newbutton2,75%x,75%y,50dip,50dip)
    newbutton.Text = "test"
    newbutton2.Text = "test"
    Dim grad As GradientDrawable
    clrs(0) = Colors.Blue
    clrs(1) = Colors.red
    grad.Initialize("LEFT_RIGHT",clrs)

    For Each v As View In Activity.GetAllViewsRecursive
        If v Is Button Then
            Dim b As Button = V
            b.Background = grad
        End If
    Next
End Sub
Sub Activity_Resume

End Sub
Sub Activity_Pause (UserClosed As Boolean)
End Sub
Sub newbutton_Click
    Dim grad As GradientDrawable
    clrs(0) = Colors.Blue
    clrs(1) = Colors.red
    grad.Initialize("LEFT_RIGHT",clrs)

    For Each v As View In Activity.GetAllViewsRecursive
        If v Is Button Then
            Dim b As Button = v
            b.TextColor = Colors.cyan
            b.Background = grad
        End If
    Next
End Sub
 
Upvote 0

kostefar

Active Member
Licensed User
Longtime User
Thanks, I had noticed in my original project (what I posted was a very cut down test of the behavour) how buttons would resize to the original whenever set to the background again, but thought that getting into solving it that way would be a wrong cure for the disease. But ok, if this is some bug and that´s the workaround I´ll just handle it that way.

Big thanks MarcoRome and wishes for a good weekend, now I can continue! :)
 
Upvote 0
Top