I feel like this is kind of a silly question, but here I go anyways. I have an image button, and I want to be able to change it's image everytime it is clicked. The API seems to say that the best way to go about doing this is to create xml resource in the drawable folder that contains a selector and values. However when I go to make a new android xml resource, there's no option for drawables. What am I missing?
Asked
Active
Viewed 6.5k times
36

Vadim Kotov
- 8,084
- 8
- 48
- 62

aamiri
- 2,420
- 4
- 38
- 58
-
you can check the **solution** here [click](https://stackoverflow.com/a/57077014/5872337) – Geet Thakur Jul 17 '19 at 13:36
2 Answers
58
You can add this in Android Studio, use Right click on project structure -> New -> Drawable resource file
. It should look like this:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="false"
android:drawable="@drawable/cell_top_selected" />
<item android:drawable="@drawable/cell_top" />
</selector>

Vadim Kotov
- 8,084
- 8
- 48
- 62

James
- 2,346
- 1
- 16
- 18
-
Should a selector (color/drawable) always have a default value? If you match all possible cases, it should be fine too, right? What happens if you don't match at all? Will the app crash? – android developer Aug 31 '23 at 12:05
37
You can try this also as a selector
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- pressed -->
<item android:drawable="@drawable/button_1_selected" android:state_pressed="true"/>
<!-- focused -->
<item android:drawable="@drawable/button_1_normal" android:state_focused="true"/>
<!-- default -->
<item android:drawable="@drawable/button_1_normal"/>
</selector>

OneCricketeer
- 179,855
- 19
- 132
- 245

dhir
- 465
- 4
- 4