How to change the color of a button?

后端 未结 8 1818
臣服心动
臣服心动 2021-02-03 20:08

I\'m new to android programming. How do I change the color of a button?

相关标签:
8条回答
  • 2021-02-03 20:32

    You can change the colour two ways; through XML or through coding. I would recommend XML since it's easier to follow for beginners.

    XML:

    <Button
        android:background="@android:color/white"
        android:textColor="@android:color/black"
    />
    

    You can also use hex values ex.

    android:background="@android:color/white"
    

    Coding:

    //btn represents your button object
    
    btn.setBackgroundColor(Color.WHITE);
    btn.setTextColor(Color.BLACK);
    
    0 讨论(0)
  • 2021-02-03 20:32

    You can change the value in the XML like this:

    <Button
        android:background="#FFFFFF"
         ../>
    

    Here, you can add any other color, from the resources or hex.

    Similarly, you can also change these values form the code like this:

    demoButton.setBackgroundColor(Color.WHITE);
    

    Another easy way is to make a drawable, customize the corners and shape according to your preference and set the background color and stroke of the drawable. For eg.

    button_background.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android">
        <stroke android:width="2dp" android:color="#ff207d94" />
        <corners android:radius="5dp" />
        <solid android:color="#FFFFFF" />
    </shape>
    

    And then set this shape as the background of your button.

    <Button
        android:background="@drawable/button_background.xml"
         ../>
    

    Hope this helps, good luck!

    0 讨论(0)
  • 2021-02-03 20:34

    Here is my code, to make different colors on button, and Linear, Constraint and Scroll Layout

    First, you need to make a custom_button.xml on your drawable

    1. Go to res
    2. Expand it, right click on drawable
    3. New -> Drawable Resource File
    4. File Name : custom_button, Click OK

    Custom_Button.xml Code

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:state_pressed="true" android:drawable="@color/red"/> <!-- pressed -->
        <item android:state_focused="true" android:drawable="@color/blue"/> <!-- focused -->
        <item android:drawable="@color/black"/> <!-- default -->
    </selector>
    

    Second, go to res

    1. Expand values
    2. Double click on colors.xml
    3. Copy the code below

    Colors.xml Code

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
        <color name="colorPrimary">#3F51B5</color>
        <color name="colorPrimaryDark">#303F9F</color>
        <color name="colorAccent">#FF4081</color>
    
        <color name="black">#000</color>
        <color name="violet">#9400D3</color>
        <color name="indigo">#4B0082</color>
        <color name="blue">#0000FF</color>
        <color name="green">#00FF00</color>
        <color name="yellow">#FFFF00</color>
        <color name="orange">#FF7F00</color>
        <color name="red">#FF0000</color>
    </resources>
    

    Screenshots below

    XML Coding Design Preview

    0 讨论(0)
  • 2021-02-03 20:42

    For the text color add:

    android:textColor="<hex color>"
    


    For the background color add:

    android:background="<hex color>"
    


    From API 21 you can use:

    android:backgroundTint="<hex color>"
    android:backgroundTintMode="<mode>"
    

    Note: If you're going to work with android/java you really should learn how to google ;)
    How to customize different buttons in Android

    0 讨论(0)
  • 2021-02-03 20:44

    To change the color of button programmatically

    Here it is :

    Button b1;
    //colorAccent is the resource made in the color.xml file , you can change it.
    b1.setBackgroundResource(R.color.colorAccent);  
    
    0 讨论(0)
  • 2021-02-03 20:48

    The RIGHT way...

    The following methods actually work.

    if you wish - using a theme
    By default a buttons color is android:colorAccent. So, if you create a style like this...

    <style name="Button.White" parent="ThemeOverlay.AppCompat">
        <item name="colorAccent">@android:color/white</item>
    </style>
    

    You can use it like this...

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:theme="@style/Button.White"
        />
    

    alternatively - using a tint
    You can simply add android:backgroundTint for API Level 21 and higher, or app:backgroundTint for API Level 7 and higher.

    For more information, see this blog.

    The problem with the accepted answer...

    If you replace the background with a color you will loose the effect of the button, and the color will be applied to the entire area of the button. It will not respect the padding, shadow, and corner radius.

    0 讨论(0)
提交回复
热议问题