Satyam Bansal
Satyam Bansal

Reputation: 383

StateList Drawable not being implemented through Styles

I am trying to change the ImageButton src attribute when it's states changes between pressed and default state (untouched). I created the below StateList Drawable named ic_plus_states -

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

    <item android:drawable="@drawable/ic_plus_white" android:state_pressed="true" />
<!--Default State-->
    <item android:drawable="@drawable/ic_plus" />

</selector>

It works as intended when I set android:src="@drawable/ic_plus_states" but when I use a style defined in styles.xml containing the exact same attribute, it doesn't works. Is there anything I am doing wrong?

I used this in my styles.xml -

<style name="ScoreButtons" parent="Widget.AppCompat.Button">
        <item name="android:tint">@color/colorPrimary</item>
        <item name="android:background">@drawable/button_states</item>
    </style>

    <style name="PlusButtons" parent="ScoreButtons">
        <item name="android:src">@drawable/ic_plus_states</item>
        <item name="android:contentDescription">@string/plus_button</item>
    </style>

Upvotes: 0

Views: 31

Answers (1)

Rajnish suryavanshi
Rajnish suryavanshi

Reputation: 3424

when I use a style defined in styles.xml containing the exact same attribute, it doesn't works.

It's because of android:tint you have defined in style ScoreButtons.

If you Remove android:tint from ScoreButtons. You will get your output as expected.

<style name="ScoreButtons" parent="Widget.AppCompat.Button">
        <item name="android:background">@drawable/button_states</item>
    </style>

    <style name="PlusButtons" parent="ScoreButtons">
        <item name="android:src">@drawable/ic_plus_states</item>
        <item name="android:contentDescription">@string/plus_button</item>
    </style>

OR

You can set android:tint="@null" in style PlusButtons.

<style name="ScoreButton" parent="Widget.AppCompat.Button">
        <item name="android:tint">@android:color/holo_red_dark</item>
        <item name="android:background">@android:color/black</item>
    </style>

    <style name="PlusButton" parent="ScoreButton">
        <item name="android:tint">@null</item>
        <item name="android:src">@drawable/state_change</item>
    </style>

Upvotes: 1

Related Questions