gabrjan gabrjan - 6 months ago 87
Android Question

Add shadow to button in android

I'm trying to add some kind of shadow over image button on click. Is it possible and how.

So i have button and i wished it would look pushed when i click it. So i created new xml like that:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_pressed="true"
android:state_enabled="true"
android:drawable="@color/btn_pushed"
/>
<item
android:drawable="@drawable/btn" />
</selector>


Ok it works fine, but i have like 50 buttons and i have to make new image for all of them. That's a lot of work, is it possible to just add some kind of shadow over it? Or something so that it will look pushed?

Answer

So i found a solution and i add it here for feature readers. The trick is to use . I found the answer here http://belencruz.com/2012/12/rounded-button-with-shadow-in-android/ My code now:

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


<item
    android:state_pressed="true"
    android:state_enabled="true"
    >
    <layer-list xmlns:android="http://schemas.android.com/apk/res/android">

<item android:drawable="@drawable/btn" /> 
<item android:drawable="@color/transparent"/>


</layer-list>
</item>
<item
    android:drawable="@drawable/btn" />
</selector>

Works realy nice!

Comments