I know that you can make a selector and set different press modes there. But after all, standard libraries are now working very nicely pressing. My question is that when I create just a button in an empty activation and click on it, it makes a beautiful wave from the touch point and further along the entire button, but when I add my button

 android:background="@drawable/stylecapturebutton" android:src="@drawable/photocamera" 

then she stops doing that ...

Here is the code of my ImageButton :

 <ImageButton android:id="@+id/bPicture" android:onClick="bPicture" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/stylecapturebutton" android:src="@drawable/photocamera" android:padding = "10dp" /> 

Is it possible to somehow set the background and resource in the button, while at the same time maintaining the standard touch processing?

Such a button turned out

 <?xml version="1.0" encoding="utf-8"?> <layer-list xmlns:android="http://schemas.android.com/apk/res/android"> <item> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval"> <!--<solid android:color="@color/myColor" />--> <stroke android:width="1dp" android:color="@color/color_white" /> <size android:width="18dp" android:height="18dp" /> </shape> </item> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@drawable/button" android:state_focused="true" android:state_pressed="false" /> <item android:drawable="@drawable/button" android:state_focused="true" android:state_pressed="true" /> <item android:drawable="@drawable/button_pressed" android:state_focused="false" android:state_pressed="true" /> <item android:drawable="@drawable/button" /> </selector> </layer-list> 
  • Unfortunately you can not - Android Android
  • @ Android Android well here ( - Aleksey Timoshchenko
  • @AndroidAndroid so that then only selector -ami to work? or is there something else? - Aleksey Timoshchenko
  • Yes, now I'll add an example to the answers - Android Android

2 answers 2

I use ripple, which allows you to add a wave effect for Views with a custom background color. 1. Create a markup file at the address - resources / drawable / button markup.xml:

 <?xml version="1.0" encoding="utf-8"?> <ripple xmlns:android="http://schemas.android.com/apk/res/android" android:color="@color/color_button_светлый_цвет"> <item> <shape xmlns:android="http://schemas.android.com/apk/res/android"> <corners android:radius="2dp" /> <solid android:color="@color/color_button_темный_цвет" /> </shape> </item> </ripple> 
  1. Register as a background link to this file:

    android: background = "@ drawable / button markup"

  2. Be careful, this trick works on Android versions after 5.0. Before them, I use selectors. To do this, you need to create default background files in drawable through selectors, and in the drawable-v21 folder, background files with ripple. Note that the drawable-v21 folder should be in resources, i.e. at the level of the drawable folder, and not inside it.

  • Fine! What you need ... And tell me again, how will the button on the version below 5.0 behave? Just change the color without a wave or there will be no response at all? - Aleksey Timoshchenko
  • Below 5.0 will behave as you specify in selectors. For now, I use 2 states in the selector: the default is one color, and when I clicked it is a different color. Color changes dramatically without a wave. Without a selector - as you described in the question, without visible changes, that the button is pressed. You can try how selectors work on version 6.0 and see for yourself, pick colors, and then create a drawable-v21 folder with ripple. - St-st
  • I probably missed something. Well I can not use your approach and selectors for one view ... After all, I set both as background, which means that I can either set the selector or riple ... Or not so? - Aleksey Timoshchenko
  • Did not notice your question. If it is still relevant or useful to others: both work simultaneously. If the user has an android up to 5.0, then selectors are active, i.e. what is contained in the drawable folder. And if the user has an Android equal to and above 5.0, then the ripple acts, i.e. what is contained in the drawable-v21 folder. - St-st

Create an xml file put it in drawable resources

 <item android:drawable="@drawable/button" android:state_focused="true" android:state_pressed="false"/> <item android:drawable="@drawable/button" android:state_focused="true" android:state_pressed="true"/> <item android:drawable="@drawable/button_pressed" android:state_focused="false" android:state_pressed="true"/> <item android:drawable="@drawable/button"/> 

And your button in the android:background attribute, specify the name of this xml.

  • Well, this is a standard selector ... And under the desired positions I substitute the desired @drawable/button , right? - Aleksey Timoshchenko
  • Yeah, that's right. - Android Android
  • And can you tell more ... Do you get selected 4 positions to which the button responds ... Can you explain at what time what works? I just don't understand, let's say the 1st ... how does focused (set when a view has input focus.) Differ from pressed? And why in paragraph 2 do you have true on these two parameters? How should this work? - Aleksey Timoshchenko
  • one
    I can not clarify in detail, I know, it's just that it works like that =) - Android Android