![]() |
VOOZH | about |
Counter Floating Action button is seen in most of the E-commerce apps where we are using the functionality to display the number of items that are present in the user's shopping cart. In this article, we will take a look at implementing this counter floating action button in our Android app in Android Studio.
We will be building a simple application in which we will be simply displaying a Floating action button and we will be adding a counter to it. On clicking on that button we will be incrementing and decrementing the counter which is applied on that Floating action button. Below is the video in which we will get to see what we are going to build in this article.
Step 1: Create a New Project
To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. Note that select Java as the programming language.
Step 2: Add dependency
Navigate to the Gradle Scripts > build.gradle(Module:app) and add the below dependency in the dependencies section.
implementation 'com.github.andremion:counterfab:1.2.2'
After adding this dependency now sync your project and we will move towards working with the XML file.
Step 3: Working with the activity_main.xml file
Navigate to the app > res > layout > activity_main.xml and add the below code to that file. Below is the code for the activity_main.xml file.
Step 4: Working with the MainActivity.java file
Go to the MainActivity.java file and refer to the following code. Below is the code for the MainActivity.java file. Comments are added inside the code to understand the code in more detail.
Now run your app and see the output of the app.
Output: