The touch feedback in Android is a must whenever the user clicks on the item or button ripple effect when clicking on the same, gives confidence to the user that the button has been clicked so that they can wait for the next interaction of the app. So in this article, we are going to discuss what type of ripples can be implemented and where it can be used. Have a look at the following image to get an idea about the discussion.
Steps to Implement the button ripples (touch feedbacks) in Android
Step 1: Create an empty activity project
- Create an empty activity Android Studio project. Refer to Android | How to Create/Start a New Project in Android Studio? to know how to create an empty activity Android Studio project.
Step 2: Working with the activity_main.xml file
- In the main layout of the application, there are 4 TextViews that have been implemented. one for the default touch feedback with border, the second is for default touch feedback without border, the third is for custom touch feedback with border, fourth is for the custom touch feedback without border.
- Invoke the following code to implement the UI.
Step 3: Default Ripples in Android
- The default ripple in the android for the buttons or the text buttons can be achieved with the tags:
- android:background=”?android:attr/selectableItemBackground”: this creates ripple effect with border.
- android:background=”?android:attr/selectableItemBackgroundBorderless”: this creates ripple effect without border.
Note: These tags are need to be set under the TextView.
- To implement both of the default ripple effects in android invoke the following code inside the activity_main.xml file.
Output: Run on Emulator
Step 4: Adding custom layouts
- These are needed to be implemented under the drawable folder. After implementing they need to set as the background for these text buttons.
- To add the layouts right click on the drawable folder > new > Drawable Resource File.
- The first layout is custom_bordered_ripple.xml and invoke the following code inside it.
- The second layout is custom_borderless_ripple.xml and invoke the following code.
- The next step is to invoke these custom layouts as the background for the Custom Ripple With Border and Custom Ripple Without Border buttons inside the activity_main.xml file.