question

njsokalski avatar image
0 Votes"
njsokalski asked ·

Using a State Selector for Nested Drawables

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?

dotnet-android
· 1
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi @njsokalski , could you please post the code of your layer-list drawable and the Vector Drawable so that we can help you better?

0 Votes 0 ·

1 Answer

njsokalski avatar image
0 Votes"
njsokalski answered ·

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>

·
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.