Change the color of a disabled button in android

后端 未结 6 1350
灰色年华
灰色年华 2021-02-03 22:56

Is there a way to change the color of a disabled button in android through styles or some other form ?

I currently have the following,

drawable/button_de

相关标签:
6条回答
  • 2021-02-03 23:27

    Try this -

    drawable/bg_button.xml :-

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
    
        <item android:drawable="@drawable/bg_button_focused" android:state_selected="true"/>
        <item android:drawable="@drawable/bg_button_pressed" android:state_pressed="true"/>
        <item android:drawable="@drawable/bg_button_disabled" android:state_enabled="false" />
        <item android:drawable="@drawable/bg_button_normal"/>
    
    </selector>
    

    After this just set background on your button like this -

    <Button
        android:id="@+id/button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/bg_button"
        android:text="Button"/>
    
    0 讨论(0)
  • 2021-02-03 23:30

    Another way to achieve this is to create a color selector.

    Create a file

    res/color/your_color.xml
    

    which looks like

    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:color="@color/colorDisabledBackground" android:state_enabled="false" />
        <item android:color="@color/colorEnabledBackground" />
    </selector>
    

    Then you may use it as a regular color

    in a style:

    <style name="YourStyle">
         <item name="android:background">@color/your_color</item>
         <item name="android:textColor">@android:color/black</item>
    </style>
    

    As well is in layouts, shapes or in code as etc.

    0 讨论(0)
  • 2021-02-03 23:31

    UPDATE: For Android MaterialButton

    For Android MaterialButton there are two solutions, one for SDK >= v21 and another for SDK < v21.

    Here I am also giving how to change the text color of the button depending whether it is enabled or disabled.

    First create two color selector .xml file in your color folder. One for button color and another for button text color. For example button_state_colors.xml for button color and button_text_state_colors.xml for text color.

    Here are those two color files:

    1. button_state_colors.xml:

       <?xml version="1.0" encoding="utf-8"?>
       <selector xmlns:android="http://schemas.android.com/apk/res/android">
           <item android:state_enabled="true" android:color="#303F9F" />
           <item android:state_enabled="false" android:color="#BBBBBB" />
       </selector>
      
    2. button_text_state_colors.xml:

       <?xml version="1.0" encoding="utf-8"?>
       <selector xmlns:android="http://schemas.android.com/apk/res/android">
           <item android:state_enabled="true" android:color="#FFFFFF" />
           <item android:state_enabled="false" android:color="#555555" />
       </selector>
      

    Now for all versions of Android SDK you can change the color of button and it's text using style like:

    in style.xml:

    <style name="Widget.AppTheme.MaterialButton" parent="Widget.MaterialComponents.Button">
        <item name="backgroundTint">@color/button_state_colors</item>
        <item name="android:textColor">@color/button_text_state_colors</item>
    </style>
    

    in layout.xml:

    <com.google.android.material.button.MaterialButton
        android:id="@+id/button"
        style="@style/Widget.AppTheme.MaterialButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
    

    Only for SDK >= 21 you can direct change button color without using style like:

    <com.google.android.material.button.MaterialButton
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:backgroundTint="@color/button_state_colors"/>
    
    0 讨论(0)
  • 2021-02-03 23:32

    Specify the color in selector for android:state_enabled="false" as drawable

    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:state_enabled="false">
            <shape>
                <solid android:color="#32ff09" />
            </shape>
        </item>
    </selector>
    

    And apply this drawable resource to background of button

     <Button
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="@drawable/drawble_name"
        android:enabled="false"
        android:text="Selector Applied Button" />
    
    0 讨论(0)
  • 2021-02-03 23:45

    You'll have to use a selector for different drawables in those states.

    You can create a selector like this:

    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:drawable="@drawable/your_enabled_drawable" android:state_enabled="true" />
        <item android:drawable="@drawable/your_disabled_drawable" android:state_enabled="false" />
        <!-- default state-->
        <item android:drawable="@drawable/your_enabled_drawable" />
    </selector>
    
    0 讨论(0)
  • 2021-02-03 23:48

    Here is my code which works properly with button enable and disable state.

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
       <item android:state_enabled="true" android:drawable="@drawable/ic_button_gradient"/>
       <item android:state_enabled="false" android:drawable="@color/gray"/>
    </selector>
    
    0 讨论(0)
提交回复
热议问题