Android: Textcolor of Disabled Button in Selector Not Showing

Android: textColor of disabled button in selector not showing?

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.

Android - Button's textColor with a selector not working

Try this one:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" android:color="@android:color/black"/>
<item android:state_pressed="true" android:color="@android:color/black"/>
<item android:color="@android:color/white"/>
</selector>

How do I set the disabled color of a button with AppCompat?

You aren't using the Widget.AppCompat.Button.Colored style correctly. You're using a parent style (Widget.AppCompat.Button.Colored), but applying it as a theme. This effectively means that the Widget.AppCompat.Button.Colored part is being ignored entirely and you are instead just changing the default color of the button (which works, but doesn't handle the disabled case).

Instead, you should use a ThemeOverlay and apply the Colored style separately:

<style name="AccentButton" parent="ThemeOverlay.AppCompat.Dark">
<!-- customize colorButtonNormal for the disable color -->
<!-- customize colorAccent for the enabled color -->
</style>

<Button
android:id="@+id/login_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/fragment_login_login_button"
android:theme="@style/AccentButton"
style="@style/Widget.AppCompat.Button.Colored"/>

As mentioned in this answer on using the Widget.AppCompat.Button.Colored style, the disabled color is controlled by colorButtonNormal and the enabled color is controlled by colorAccent. By using the ThemeOverlay.AppCompat.Dark, the textColor is automatically changed to dark, meaning you may not need the custom ThemeOverlay at all.

Change the color of a disabled button in android

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>

Textcolor selector not working

Make sure your TextView is ready for listening the states you are applying for.
For instance, to be able to reach the "state_pressed" your textView should be clickable:

android:clickable="true"

EDIT: There we go. This layout does the job. Note that the View that gathers the click event is the linearLayout, but the TextView reproduces it because of "duplicateParentState" set to true. The color selector would take care of the colors for the different states.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/testLlayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:clickable="true" >

<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@drawable/textview_selector"
android:duplicateParentState="true"
android:text="TextView" />

</LinearLayout>

And here is the code for the color selector.

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

<item android:state_selected="true" android:color="#444"/>
<item android:state_focused="true" android:color="#444"/>
<item android:state_pressed="true" android:color="#444"/>
<item android:color="#ccc"/>

</selector>

That should be it.



Related Topics



Leave a reply



Submit