Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add shadow to button in android

Tags:

android

button

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?

like image 348
gabrjan Avatar asked Oct 04 '22 20:10

gabrjan


1 Answers

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!

like image 136
gabrjan Avatar answered Oct 07 '22 18:10

gabrjan