Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Implementing Ripple Effects for Android Buttons

Tech Apr 15 9

For rectangular buttons, use the selectableItemBackground attribute in the foreground to apply a default ripple effect.

<TextView
    android:id="@+id/btn_submit"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#06CD5D"
    android:foreground="?selectableItemBackground"
    android:text="Submit" />

When using a custom rounded background, the ripple effect may not match the buton's shape. To create a rounded button with a matching ripple, define a ripple drawable.

Create a shape drawable for the button background:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <solid android:color="#06CD5D" />
    <corners android:radius="25dp" />
</shape>

Define a ripple drawable that references the shape:

<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
    android:color="#22000000">
    <item android:drawable="@drawable/rounded_button_background" />
</ripple>

Apply the ripple drawable as the button's background:

<TextView
    android:id="@+id/btn_action"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/ripple_rounded_button"
    android:text="Action" />

This approach ensures the ripple effect conforms to the button's rounded corners, porviding a consistent visual feedback during user interaction.

Related Articles

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

SBUS Signal Analysis and Communication Implementation Using STM32 with Fus Remote Controller

Overview In a recent project, I utilized the SBUS protocol with the Fus remote controller to control a vehicle's basic operations, including movement, lights, and mode switching. This article is aimed...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.