Android Button ==> Set highlighted image?
-
28-09-2019 - |
Pergunta
How can i set the button to show a different image after it's been tapped? Either a different image, or maybe some kind of highlighting that shows the button was tapped/
Thanks
Solução
When you define a ImageButton on your layout you can set different images for different states: normal, selected and pressed; using a state drawable, defined in xml, like this (btn_state.xml
, under res/drawable directory):
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:drawable="@drawable/btn_pressed" />
<item android:state_focused="true"
android:drawable="@drawable/btn_selected" />
<item
android:drawable="@drawable/btn_normal" />
</selector>
Of course, you need those btn_normal, selected and pressed as PNGs in your drawables directory.
On your <ImageButton>
then you set that resource name as the background:
<ImageButton
...
android:background="@drawable/btn_state"
... />
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow