Android Question How can I change the background color of a button

Discussion in 'Android Questions' started by Mashiane, Jul 17, 2015.

  1. Mashiane

    Mashiane Expert Licensed User

    Hi

    I'd like to change the default color of a button to be blue for example. How can I do this easily?
     
  2. MaFu

    MaFu Well-Known Member Licensed User

    For general change:
    button.Color = Colors.Blue
    But in this case the button is blue in all states.
    To use different colors for the button states (disabled, pressed, normal, ...) you must use StateListDrawable and ColorDrawables.
    See example here: link
     
  3. Mashiane

    Mashiane Expert Licensed User

    Thanks a lot, I was looking for a property in the designer. I'm assigning an RGB value like this

    cmdLogin.Color = Colors.RGB(154,217,234)

    but this seems to take away the corner radius/rounded corners, do you have any idea to fix that.
     
  4. Mashiane

    Mashiane Expert Licensed User

    Found it,

    Code:
    Sub PowderBlueButton(btn As Button)
        
    Dim dr As ColorDrawable
        dr.Initialize(
    Colors.rgb(154,217,234), 2%x)
        btn.Background = dr
    End Sub
     
    RandomCoder likes this.
  5. RandomCoder

    RandomCoder Well-Known Member Licensed User

    To make your sub more reusable it would be better to pass the required color as a parameter. But if always wanting the same colour then of course you can stick with what you have already. ;)
     
    Mashiane likes this.
  6. Mashiane

    Mashiane Expert Licensed User

    Definately...
     
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