Button click effect

4

I wonder if it's possible for a Button to have that click effect, that is, it "sinks" and "returns to normal". Is this a property or should I rather use an ImageButton with the images of this "effect"? Below is my control XML that I created in drawable:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true">
        <shape>
            <solid android:color="@color/cardview_dark_background" />
            <padding android:bottom="3dp" android:left="3dp" android:right="3dp" android:top="3dp" />
        </shape>
    </item>
    <item android:state_focused="true">
        <shape>
            <solid android:color="@android:color/transparent" />
            <padding android:bottom="3dp" android:left="3dp" android:right="3dp" android:top="3dp" />
        </shape>
    </item>
    <item android:state_enabled="false">
        <shape>
            <solid android:color="@android:color/transparent" />
            <padding android:bottom="3dp" android:left="3dp" android:right="3dp" android:top="3dp" />
        </shape>
    </item>
    <item>
        <shape>
            <solid android:color="@android:color/darker_gray" />
            <padding android:bottom="3dp" android:left="3dp" android:right="3dp" android:top="3dp" />
        </shape>
    </item>
</selector>
    
asked by anonymous 16.04.2016 / 19:33

2 answers

2

You need to create a different Shape for the "state":

button_selected.xml :

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <solid android:color="#DBDBDB" />
    <corners android:radius="14dp" />
    <stroke
        android:width="1dp"
        android:color="#DBDBDB" />
</shape>

botao_normal.xml :

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <solid android:color="#F2F2F2" />
    <corners android:radius="14dp" />


    <stroke
        android:width="1dp"
        android:color="#DBDBDB" />
</shape>

And the drawable with the button itself, botao.xml :

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/botao_selecionado" android:state_pressed="true" /> <!-- pressionado -->
    <item android:drawable="@drawable/botao_selecionado" android:state_focused="true" /> <!-- com foco -->
    <item android:drawable="@drawable/botao_normal" /> <!-- normal-->
</selector>

Then in your layout you only assign botao.xml as background:

android:background="@drawable/botao"

    
07.11.2016 / 16:51
2

Looking at what you put in the question, drawable is missing the following:

Code:

 <item android:state_pressed="false">
        <shape>
            <solid android:color="@color/cardview_dark_background" /><!-- USE OUTRA COR! -->
            <padding android:bottom="3dp" android:left="3dp" android:right="3dp" android:top="3dp" />
        </shape>
    </item>
    
20.04.2016 / 01:18