Android: textColor of disabled button in selector not showing?

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

  • Enabled / Disabled
  • Press / Not Pressed

In accordance with the above conditions. I need to manipulate a button:

  • Text color
  • background image

The button starts to turn off, so it must have textColor turned off and the background of the button turned off. But I can see the default textColor text (specified in the style) and NO background image!

Here is my button_selector.xml selector

<?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 the declaration of my button in 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 textColor text 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!

+61
android xml button selector background-image
Jun 27 '12 at 11:27
source share
5 answers

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

Follow these steps:

  • 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> 
  • In style.xml put the link to this text_color.xml file as follows:

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

This should solve your problem.

+170
Jun 27 '12 at 11:50
source share

The easiest solution is to set the color filter on the background image and button, as I saw here

You can do the following:

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

Hope I helped someone ...

+4
Sep 29 '13 at 18:34
source share

1.Create a color folder in the / res / folder and in the color folder create in 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 the 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"/> 
+3
Jul 09 '15 at 10:30
source share
 <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" /> 

I do not see the layout of your button in your xml layout. add this to the button layout.

 android:enabled="false" 

so your button layout will be,

 <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:enabled="false" android:paddingRight="15dp" android:layout_gravity="left" style="@style/buttonStyle" android:background="@drawable/button_selector" /> 
0
Jun 27 '12 at 11:40
source share
 <item android:state_enabled="true"> </item> <item android:state_enabled="false"> </item> <item android:state_pressed="true" > </item> 

Hope it works!

0
Oct 29 '15 at 5:56
source share



All Articles