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

Ripple Effect on Android Button

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:

1. Using Default Ripple Effect:

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"/>

2. Using Custom Ripple Effect:

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"/>

Notes:

  • 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.

  1. Implementing Ripple Effect in Android Button:

    • Description: Introduces the concept of the Ripple Effect and how to apply it to Android buttons for a touch feedback animation.
    • Example Code:
      <Button
          android:id="@+id/myButton"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="Click me"
          android:background="?attr/selectableItemBackground"/>
      
  2. Customizing Ripple Effect on Android Button:

    • Description: Explores ways to customize the appearance and behavior of the Ripple Effect on Android buttons.
    • Example Code:
      <Button
          android:id="@+id/myButton"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="Click me"
          android:background="?attr/selectableItemBackgroundBorderless"/>
      
  3. RippleDrawable in Android example code:

    • Description: Demonstrates the use of RippleDrawable, providing more control over the Ripple Effect.
    • 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"/>
      
  4. Material Design Ripple Effect on Button Android:

    • Description: Shows how to achieve the Material Design Ripple Effect on Android buttons for a consistent and modern UI.
    • Example Code:
      <Button
          android:id="@+id/myButton"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="Click me"
          android:background="?attr/selectableItemBackground"/>
      
  5. Changing ripple color in Android Button:

    • Description: Guides on changing the color of the Ripple Effect to match the app's color scheme.
    • Example Code:
      <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"/>
      
  6. Button click animation with Ripple Effect in Android:

    • Description: Combines the Ripple Effect with a button click animation for enhanced user feedback.
    • Example Code:
      <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"/>
      
  7. Ripple Effect on ImageButton in Android:

    • Description: Applies the Ripple Effect to ImageButtons for touch feedback on image-based buttons.
    • Example Code:
      <ImageButton
          android:id="@+id/myImageButton"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:src="@drawable/icon"
          android:background="?attr/selectableItemBackground"/>
      
  8. Ripple Effect on FloatingActionButton in Android:

    • Description: Demonstrates how to add the Ripple Effect to Floating Action Buttons (FABs).
    • Example Code:
      <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"/>
      
  9. Ripple Effect on custom buttons in Android:

    • Description: Applies the Ripple Effect to custom-styled buttons, ensuring a consistent UI across the app.
    • Example Code:
      <Button
          android:id="@+id/myCustomButton"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          style="@style/MyCustomButtonStyle"
          android:background="?attr/selectableItemBackground"/>
      
  10. Using XML to define Ripple Effect in Android Button:

    • Description: Shows how to define the Ripple Effect in XML, providing more control and reusability.
    • Example Code:
      <!-- 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"/>
      
  11. Programmatic implementation of Ripple Effect on Android Button:

    • Description: Implements the Ripple Effect programmatically in the Java/Kotlin code.
    • Example Code (Kotlin):
      val myButton: Button = findViewById(R.id.myButton)
      val rippleDrawable = ContextCompat.getDrawable(this, R.drawable.custom_ripple) as RippleDrawable
      myButton.background = rippleDrawable
      
  12. Ripple Effect on Android Button with Kotlin:

    • Description: Shows how to implement the Ripple Effect on Android buttons using Kotlin.
    • Example Code:
      val myButton: Button = findViewById(R.id.myButton)
      myButton.background = ContextCompat.getDrawable(this, R.drawable.custom_ripple)
      
  13. Handling button states and Ripple Effect in Android:

    • Description: Discusses how to manage different button states (pressed, focused, etc.) along with the Ripple Effect.
    • Example Code:
      <Button
          android:id="@+id/myButton"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="Click me"
          android:background="?attr/selectableItemBackground"/>