0

I want the onClick() animation to be there on the buttons.

I have already tried

myButton.setBackgroundResource(0) 

and

myButton.setBackgroundColor(Color.TRANSPARENT)

but both of these disable the onClick() animation.

Lamorak
  • 10,957
  • 9
  • 43
  • 57
Isj
  • 2,020
  • 1
  • 14
  • 20

2 Answers2

7

You can create a drawable with <selector> if you need to have control over your clickable item design.

If you only need to make your view transparent and apply system default pressed color, you can use AppCompat's selectableItemBackground as following:

TypedValue outValue = new TypedValue();
getContext().getTheme().resolveAttribute(R.attr.selectableItemBackground, outValue, true);
myButton.setBackgroundResource(outValue.resourceId);

or in XML:

android:background="?attr/selectableItemBackground"

Check this question for further information.

Community
  • 1
  • 1
Lamorak
  • 10,957
  • 9
  • 43
  • 57
1

You can create selector in drawable and can set as a button background.Here you can specify its stats pressed,focused etc.

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
     <item android:state_pressed="true" >
         <shape android:shape="rectangle"  >
             <corners android:radius="3dip" />
             <stroke android:width="1dip" android:color="@color/theme_text_color" />
             <gradient  android:angle="-90"  android:startColor="@color/blue_transparent" android:endColor="@color/blue_transparent"  />            
         </shape>
     </item>
    <item android:state_focused="true">
         <shape android:shape="rectangle"  >
             <corners android:radius="3dip" />
             <stroke android:width="1dip" android:color="@color/theme_text_color" />
             <solid  android:color="@color/theme_text_color"/>       
         </shape>
     </item>  
    <item >
        <shape android:shape="rectangle"  >

             <stroke android:width="1dip" android:color="#00000000" />
             <gradient  android:angle="-90"  android:startColor="#00000000" android:endColor="#00000000" />            
         </shape>
     </item>
</selector>

In xml file

android:background="@drawable/selector_button"

or programatically

myButton.setBackgroundResource(R.drawable.selector_button);
Lamorak
  • 10,957
  • 9
  • 43
  • 57
Harry Mad
  • 488
  • 5
  • 17