Android Tutorial
Software Setup and Configuration
Android Studio
File Structure
Components
Core Topics
Layout
View
Button
Intent and Intent Filters
Toast
RecyclerView
Fragments
Adapters
Other UI Component
Image Loading Libraries
Date and Time
Material Design
Bars
Working with Google Maps
Chart
Animation
Database
Advance Android
Jetpack
Architecture
App Publish
App Monetization
The ripple effect is a touch feedback response seen on many Android UI elements, signaling that an element has been touched. Here's how you can apply a ripple effect to a button or any other view in Android:
If you're okay with the default ripple effect color (which is usually a light gray), you can easily get this effect by setting the button's background to a drawable that has a ripple.
For a Button
:
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Click Me" android:background="?attr/selectableItemBackground"/>
For an ImageButton
or ImageView
:
<ImageButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/your_icon" android:background="?attr/selectableItemBackgroundBorderless"/>
If you want a custom ripple color, you can define a custom ripple drawable.
First, create a new drawable resource file (e.g., button_ripple.xml
):
<!-- res/drawable/button_ripple.xml --> <ripple xmlns:android="http://schemas.android.com/apk/res/android" android:color="@color/yourRippleColor"> <item android:id="@android:id/mask"> <shape android:shape="rectangle"> <solid android:color="@color/yourRippleColor" /> </shape> </item> <item android:drawable="@color/buttonNormalColor" /> </ripple>
Then, set this drawable as the background of your button:
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Click Me" android:background="@drawable/button_ripple"/>
Custom Ripple Colors: In the example above, you'll replace @color/yourRippleColor
and @color/buttonNormalColor
with your desired colors.
Ripple Behavior: The ripple effect starts from the point of the touch event and then spreads outward. If the view has defined boundaries (like a Button
), the ripple will be constrained to those boundaries. If the view doesn't have strict boundaries (like an ImageButton
), using selectableItemBackgroundBorderless
will let the ripple spread outside the view's bounds.
Minimum API Level: The ripple effect is available starting from API level 21 (Lollipop). If you're supporting earlier versions, make sure to provide alternative drawables or use conditional checks to avoid crashes on older devices.
By incorporating the ripple effect, you provide tactile feedback to users, making the UI more intuitive and aligned with modern Android design principles.
Implementing Ripple Effect in Android Button:
<Button android:id="@+id/myButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Click me" android:background="?attr/selectableItemBackground"/>
Customizing Ripple Effect on Android Button:
<Button android:id="@+id/myButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Click me" android:background="?attr/selectableItemBackgroundBorderless"/>
RippleDrawable in Android example code:
<Button android:id="@+id/myButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Click me" android:background="@drawable/custom_ripple"/>
Material Design Ripple Effect on Button Android:
<Button android:id="@+id/myButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Click me" android:background="?attr/selectableItemBackground"/>
Changing ripple color in Android Button:
<Button android:id="@+id/myButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Click me" android:background="?attr/selectableItemBackground" android:backgroundTint="@color/rippleColor"/>
Button click animation with Ripple Effect in Android:
<Button android:id="@+id/myButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Click me" android:background="?attr/selectableItemBackground" android:onClick="onButtonClick"/>
Ripple Effect on ImageButton in Android:
<ImageButton android:id="@+id/myImageButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/icon" android:background="?attr/selectableItemBackground"/>
Ripple Effect on FloatingActionButton in Android:
<com.google.android.material.floatingactionbutton.FloatingActionButton android:id="@+id/myFab" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/ic_add" android:background="?attr/selectableItemBackground"/>
Ripple Effect on custom buttons in Android:
<Button android:id="@+id/myCustomButton" android:layout_width="wrap_content" android:layout_height="wrap_content" style="@style/MyCustomButtonStyle" android:background="?attr/selectableItemBackground"/>
Using XML to define Ripple Effect in Android Button:
<!-- res/drawable/ripple_effect.xml --> <ripple xmlns:android="http://schemas.android.com/apk/res/android" android:color="?attr/colorControlHighlight"> <item android:id="@android:id/mask"> <shape android:shape="rectangle"> <solid android:color="?attr/colorPrimary"/> </shape> </item> </ripple> <!-- Using in a Button --> <Button android:id="@+id/myButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Click me" android:background="@drawable/ripple_effect"/>
Programmatic implementation of Ripple Effect on Android Button:
val myButton: Button = findViewById(R.id.myButton) val rippleDrawable = ContextCompat.getDrawable(this, R.drawable.custom_ripple) as RippleDrawable myButton.background = rippleDrawable
Ripple Effect on Android Button with Kotlin:
val myButton: Button = findViewById(R.id.myButton) myButton.background = ContextCompat.getDrawable(this, R.drawable.custom_ripple)
Handling button states and Ripple Effect in Android:
<Button android:id="@+id/myButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Click me" android:background="?attr/selectableItemBackground"/>