Using a State Selector for Nested Drawables

asked 2021-03-21T18:31:38.177+00:00
Nathan Sokalski 3,976 Reputation points

I am relatively new to using a selector for modifying properties for states such as enabled/disabled, etc. I have learned to use them for direct properties (such as textColor, background, etc.) in the layout files (such as activity_main.xml) & in styles (in styles.xml), but for things such as nested drawables I am not quite sure what to do. For example, I have a Button whose background is a layer-list drawable, and one of the drawables in that layer-list is a VectorDrawable, which has a path whose fillColor I need to change based on the enabled state of my Button. How do I do this?

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
4,803 questions
{count} votes

Accepted answer
  1. answered 2021-03-22T17:46:48.173+00:00
    Nathan Sokalski 3,976 Reputation points

    I'm not sure what I was doing wrong before or why I didn't think it would work (I'm still very new to drawables & selectors), but here it is now and it works:

    IncDecButtonBackground.xml:

    <?xml version="1.0" encoding="utf-8" ?>  
    <layer-list xmlns:android="http://schemas.android.com/apk/res/android">  
    	<item>  
    		<shape android:shape="rectangle">  
    			<solid android:color="@color/Silver"/>  
    			<stroke android:width="2dp" android:color="@color/Black"/>  
    			<corners android:radius="0dp"/>  
    		</shape>  
    	</item>  
    	<item android:gravity="fill" android:left="10dp" android:top="10dp" android:right="10dp" android:bottom="10dp">  
    		<vector android:width="30dp" android:height="30dp" android:viewportWidth="30" android:viewportHeight="30">  
    			<path android:pathData="M0 0L30 0L15 30Z" android:fillColor="@drawable/incdecbuttonarrowcolorselector"/>  
    		</vector>  
    	</item>  
    </layer-list>  
    

    IncDecButtonArrowColorSelector.xml:
    <?xml version="1.0" encoding="utf-8" ?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_enabled="true" android:color="@Color /Black"/>
    <item android:state_enabled="false" android:color="@Color /Gray"/>
    </selector>

    No comments

0 additional answers

Sort by: Most helpful