5

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?

gabrjan
  • 3,080
  • 9
  • 40
  • 69

3 Answers3

5

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!

gabrjan
  • 3,080
  • 9
  • 40
  • 69
  • 1
    NOTE: Google says "Don't. Shadows are never approximated by coloring material." http://www.google.com/design/spec/what-is-material/material-properties.html#material-properties-physical-properties – Don Larynx May 21 '15 at 11:17
0

Try declaring background as shadow effect and source as button image:

   <ImageButton
        android:background="@android:drawable/dialog_holo_light_frame"
        android:src="@drawable/books"
   />
-1

The above solution didn't work for me somehow. Nothing would display in place of the button. The following is my code that worked:

First: add into layout file:

android:background="@drawable/new_button"

Second: Create the new_button.xml file, where show3 is the image file

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
  <item android:state_pressed="true">
    <layer-list>
      <item android:left="4dp" android:top="4dp">
        <bitmap android:src="@drawable/show3"/>                
      </item>
    </layer-list>
  </item>
  <item>
    <layer-list>
      <!-- SHADOW LAYER -->
      <item android:left="4dp" android:top="4dp">
        <shape>
          <solid android:color="#66000000" />
          <corners android:radius="10dip"/>
        </shape>
      </item>
      <!-- CONTENT LAYER -->
      <item android:bottom="4dp" android:right="4dp">
        <bitmap android:src="@drawable/show3"/>            
      </item>
    </layer-list>
  </item>
</selector>

Basically had to add bitmap to item.

Yury Imashev
  • 2,068
  • 1
  • 18
  • 32