Android Question Button colors

Edu Chamon

Member
Licensed User
Good morning people,
Sorry for English.

Please would like to simulate 4 color changing buttons quickly, similar to the genius game.

I tried to do that and it didn't work out.

for i=1 to 300
btn1.colors = colors.red
sleep(3000)

btn2.colors = colors.green
sleep(3000)

btn3.colors = colors.yellow
sleep(3000)

btn4.colors = colors.blue
sleep(3000)
next


can anyone guide me?
Thank you
 

Attachments

  • genius.jpg
    genius.jpg
    37.3 KB · Views: 104

DonManfred

Expert
Licensed User
Please use [CODE]code here...[/CODE] tags when posting code.

codetag001.png

codetag002.png

codetag003.png


This should bring you in the right direction or at least you can get some inspiration in the Code

B4X:
Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.

    Private btnRed As Button
    Private btnGreen As Button
    Private btnBlue As Button
    Private btnYellow As Button
    Private btnStart As Button
    Private btns(4) As Button
End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    Activity.LoadLayout("Layout1")
    btns(0) = btnRed
    btns(1) = btnGreen
    btns(2) = btnBlue
    btns(3) = btnYellow
End Sub
Sub Activity_Resume
End Sub
Sub Activity_Pause (UserClosed As Boolean)
End Sub
Sub btnStart_Click
    Log($"btnStart_Click()"$)
    Dim r As Int = Rnd(0,4) ' Which Button to change? 0 to 3
    Log($"RandomButton # ${r}"$)
    
    Dim btn As Button = btns(r) ' Get reference to this Button
    Log($"ButtonText = ${btn.Text}"$)
    Dim color As Int = btn.TextColor ' remember textcolor
    btn.TextColor = Colors.White ' Set Textcolor to WHITE
    Sleep(250) ' Short sleep
    btn.TextColor = color ' Restore old Textcolor
    Sleep(250) ' Another short sleep
End Sub
 

Attachments

  • GeniusTest.zip
    9.3 KB · Views: 99
Upvote 0

DonManfred

Expert
Licensed User
Another example showing a Sequence to "Play"

B4X:
Sub btnStart_Click
    Log($"btnStart_Click()"$)
    Dim seq() As Int = Array As Int(0,3,2,1,1,2,3) ' Sequence of Buttons to show

    Dim time As Int = 500 ' MS to show a Color

    For Each key As Int In seq
        Dim r As Int = key
        Log($"Button # ${r}"$)
        Dim btn As Button = btns(r)
        Log($"ButtonText = ${btn.Text}"$)
        Dim color As Int = btn.TextColor
        btn.TextColor = Colors.White
        Sleep(time)
        btn.TextColor = color
        Sleep(time)
          
    Next
  
End Sub

record.gif
 
Last edited:
Upvote 0

Edu Chamon

Member
Licensed User
The example was clear. A doubt if possible.
I created in layout btn1, btn2, btn3, btn4. Could everyone call btn and use it as an array?
 
Upvote 0

Edu Chamon

Member
Licensed User
Now that I saw the picture, it was excellent.
But can the same apply to the button color instead of the text?
Thank you for the patience
 
Upvote 0

DonManfred

Expert
Licensed User
B4X:
Sub btnStart_Click
    Log($"btnStart_Click()"$)
    Dim seq() As Int = Array As Int(0,3,2,1,1,2,3) ' Sequence of Buttons to show

    Dim time As Int = 500 ' MS to show a Color

    For Each key As Int In seq
        Dim r As Int = key
        Log($"Button # ${r}"$)
        Dim btn As Button = btns(r)
        Log($"ButtonText = ${btn.Text}"$)
        Dim enabled As ColorDrawable

        Select r
            Case 0
                'Log("Red")
                enabled.Initialize(Colors.Red, 10dip)
            Case 1
                'Log("Green")
                enabled.Initialize(Colors.Green, 10dip)
            Case 2
                'Log("Blue")
                enabled.Initialize(Colors.Blue, 10dip)
            Case 3
                'Log("Yellow")
                enabled.Initialize(Colors.Yellow, 10dip)
        End Select
        
        Dim unchecked As ColorDrawable
        unchecked.Initialize(Colors.White, 10dip)

        
        Dim sld As StateListDrawable
        sld.Initialize
        sld.AddState(sld.State_Enabled, enabled)
        sld.AddState(sld.State_Disabled, unchecked)
         btn.Background = sld
        
        btn.Enabled = False
        Sleep(time)
        btn.Enabled = True
        Sleep(time)
            
    Next

record.gif
 

Attachments

  • GeniusTest.zip
    9.6 KB · Views: 98
Upvote 0
Top