Android Question Change border color button in code

Discussion in 'Android Questions' started by bluedude, Feb 24, 2015.

  1. bluedude

    bluedude Well-Known Member Licensed User

    Hi,

    I have set the border color of a button in the designer but now I need to change it dynamically in code, how to do that?
     
  2. eurojam

    eurojam Well-Known Member Licensed User

    Code:
    button1.Color = Colors.red
     
  3. bluedude

    bluedude Well-Known Member Licensed User

    Sorry, wrong answer. Borders are only available in statelistdrawable mode and colordrawable.
     
    eurojam likes this.
  4. Mahares

    Mahares Well Known Member Licensed User

    If yu have the new version of B4a, you can use:
    Code:
    Dim cd As ColorDrawable
            cd.Initialize2(
    Colors.Red,10dip,5dip,Colors.Green)       
            MyRB.Background=cd
     
    kopaingsoe and thedesolatesoul like this.
  5. bluedude

    bluedude Well-Known Member Licensed User

    Thanks, it works.
     
  6. bluedude

    bluedude Well-Known Member Licensed User

    Mm, does not work as expected. When pressing the button there is no pressed default effect.
     
  7. klaus

    klaus Expert Licensed User

    You need to define a StateList Drawable, and set a colordrawable for each state.
     
  8. bluedude

    bluedude Well-Known Member Licensed User

    I have done that but still some issues with pressed. See below:

    Dim btnThemeBlue,btnThemeWhite,btnThemeWhiteClicked As ColorDrawable
    Dim sldBlue,sldWhite As StateListDrawable

    sldBlue.Initialize
    sldWhite.Initialize

    btnThemeBlue.Initialize2(Colors.RGB (23, 126, 251),5dip,1dip,Colors.RGB (23, 126, 251))
    btnThemeWhite.Initialize2(Colors.White,5dip,1dip,Colors.RGB (23, 126, 251))
    btnThemeWhiteClicked.Initialize2(Colors.RGB (169, 169, 169),5dip,1dip,Colors.RGB (23, 126, 251))

    sldBlue.AddState (sldBlue.State_Enabled,btnThemeBlue)
    sldBlue.AddState (sldBlue.State_Pressed ,btnThemeWhiteClicked)

    sldWhite.AddState (sldWhite.State_Enabled,btnThemeWhite)
    sldWhite.AddState (sldWhite.State_Pressed ,btnThemeWhiteClicked)

    btnTest.Background = sldWhite
     
  9. klaus

    klaus Expert Licensed User

    Invert Enabled and Pressed:
    sldWhite.AddState (sldWhite.State_Pressed ,btnThemeWhiteClicked)
    sldWhite.AddState (sldWhite.State_Enabled,btnThemeWhite)
     
    Erel and thedesolatesoul like this.
  10. bluedude

    bluedude Well-Known Member Licensed User

    Alright, that's it :) The devil is in the details :)
     
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