Android Question Button colors

Discussion in 'Android Questions' started by Edu Chamon, Aug 6, 2019.

  1. Edu Chamon

    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
     

    Attached Files:

  2. DonManfred

    DonManfred Expert Licensed User

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

    [​IMG]
    [​IMG]
    [​IMG]

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

    Code:
    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(4As 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
     

    Attached Files:

    emexes, Erel and José J. Aguilar like this.
  3. Edu Chamon

    Edu Chamon Member Licensed User

    Very nice, thank you so much, DonManfred
     
  4. DonManfred

    DonManfred Expert Licensed User

    Another example showing a Sequence to "Play"

    Code:
    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: Aug 6, 2019
  5. Edu Chamon

    Edu Chamon Member Licensed User

    I will test. Thank you one more time
     
  6. Edu Chamon

    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?
     
  7. Edu Chamon

    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
     
  8. DonManfred

    DonManfred Expert Licensed User

    Code:
    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
     

    Attached Files:

  9. Edu Chamon

    Edu Chamon Member Licensed User

    fantastic, thank you very much
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice