Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change disabled button's text color [duplicate]

I am trying to make a button with a selector my button can have the following states:

  • Enabled/Disabled
  • Pressed/Not Pressed

According to the states mentioned above. I need to manipulate the button's:

  • Text color
  • background image

The button starts off my being disabled so it should have the disabled textColor and the disabled button background. But I can see the default textColor (specified in style) and NO background image!

Here is my selector button_selector.xml

<?xml version="1.0" encoding="utf-8"?>

<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:state_pressed="false"
        android:state_enabled="false"
        android:textColor="#9D9FA2"
        android:drawable="@drawable/button" />    

    <item android:state_pressed="true"
        android:state_enabled="true"
        android:drawable="@drawable/button_pressed"/>

    <item android:state_pressed="true"
        android:state_enabled="false"
        android:textColor="#9D9FA2"
        android:drawable="@drawable/button"/>

    <item android:state_pressed="false"
        android:state_enabled="true"
        android:drawable="@drawable/button"/>    

</selector>

And here is my button declaration in the my layout.xml

    <Button android:id="@+id/reserve_button"
        android:text="@string/reserve_button"
        android:layout_width="120dp"
        android:layout_height="40dp"
        android:layout_marginTop="10dp"
        android:layout_marginLeft="20dp"
        android:paddingRight="15dp"
        android:layout_gravity="left"
        style="@style/buttonStyle"
        android:background="@drawable/button_selector" />

And finally this is my style (where my default textColor is set)

<?xml version="1.0" encoding="utf-8"?>

 <resources>

     <style name="buttonStyle">
      <item name="android:textStyle">bold</item>
      <item name="android:textColor">#282780</item>
      <item name="android:textSize">18sp</item>
     </style>

</resources>

Please help!

like image 778
Nouran H Avatar asked Jun 27 '12 11:06

Nouran H


3 Answers

You need to also create a ColorStateList for text colors identifying different states.

Do the following:

  1. Create another XML file in res\color named something like text_color.xml.

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
      <!-- disabled state -->
      <item android:state_enabled="false" android:color="#9D9FA2" /> 
      <item android:color="#000"/>
    </selector>
    
  2. In your style.xml, put a reference to that text_color.xml file as follows:

    <style name="buttonStyle" parent="@android:style/Widget.Button">
      <item name="android:textStyle">bold</item>
      <item name="android:textColor">@color/text_color</item>
      <item name="android:textSize">18sp</item>
    </style>
    

This should resolve your issue.

like image 77
Adil Soomro Avatar answered Sep 18 '22 05:09

Adil Soomro


1.Create a color folder in /res/ folder and in color folder create on xml:

text_color_selector.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
 <!-- disabled state --> 
<item android:state_enabled="false" android:color="#776678" /> 
 <item android:color="#ffffff"/>
</selector>

2.Now Create a xml layout:-

 <Button

        android:id="@+id/button_search"

        android:layout_width="652dp"

        android:layout_height="48dp"

        android:layout_alignParentLeft="true"

        android:layout_alignParentTop="true"

        android:layout_marginTop="18dp"

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

        android:text="Hello Bhaskar"

        android:textColor="@color/text_color_selector"/>  
like image 31
Bhaskar Kumar Singh Avatar answered Sep 21 '22 05:09

Bhaskar Kumar Singh


The most easy solution is to set color filter to the background image of and button as I saw here

You can do as follow:

if ('need to set button disable')
    button.getBackground().setColorFilter(Color.GRAY, PorterDuff.Mode.MULTIPLY);
else
    button.getBackground().setColorFilter(null);

Hope I helped someone...

like image 35
Woody Avatar answered Sep 18 '22 05:09

Woody