How do I assign click effects to an ImageButton? [closed]

0

For example, I added to my xml layout an ImageButton, I put an image and left it with transparent background, for doing so it lost the characteristics of a button, how do I add click effect?

    
asked by anonymous 07.06.2015 / 17:54

1 answer

3

You need to create a drawable for it, stating the colors for the respective states. To do this, create an .xml file inside the drawable folder:

<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:state_pressed="true">
        <shape>
            <solid android:color="@color/sua_cor_selecionado" />
            <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/transparent" />
            <padding android:bottom="3dp" android:left="3dp" android:right="3dp" android:top="3dp" />
        </shape>
    </item>

</selector>

And, after creating, type in your ImageButton :

<ImageButton
    android:id="@+id/seu_botao"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/seu_arquivo_criado"/>
    
08.06.2015 / 01:09