How to grey out a button?

后端 未结 9 730
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-22 22:23

I have a button defined as shown below. When I want to disable it I use my_btn.setEnabled(false), but I would also like to grey it out. How can I do that?

相关标签:
9条回答
  • 2020-12-22 22:57

    I tried the above solutions but none of them seemed to work for me. I went with the following option:

    <!-- button_color_selector.xml -->
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:color="@color/colorAccent" android:state_enabled="true"/>
        <item android:color="@color/colorAccentLight" android:state_enabled="false"/>
    </selector>
    
    <com.google.android.material.button.MaterialButton
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:backgroundTint="@color/button_color_selector"
                    .../>
    
    0 讨论(0)
  • 2020-12-22 23:01

    Set Clickable as false and change the backgroung color as:

    callButton.setClickable(false);
    callButton.setBackgroundColor(Color.parseColor("#808080"));
    
    0 讨论(0)
  • 2020-12-22 23:03

    You have to provide 3 or 4 states in your btn_defaut.xml as a selector.

    1. Pressed state
    2. Default state
    3. Focus state
    4. Enabled state (Disable state with false indication; see comments)

    You will provide effect and background for the states accordingly.

    Here is a detailed discussion: Standard Android Button with a different color

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