Click on button effect

Asked

Viewed 2,872 times

4

I wonder if it is possible, in a Button, to have that click effect, that is, it "sink" and "return to normal". Is that a property or should I use an Imagebutton with the images of this "effect"? Below is my control XML 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>

2 answers

2

You need to create a different shape for the "status":

boot 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>

boot.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 on it, boot.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>

So in your layout is just you assign the botao.xml as a background:

android:background="@drawable/botao"

2

Analyzing what you put in the question, the drawable 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>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.