Counter Floating Action Button in Android with Example
Last Updated :
05 May, 2021
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.
What we are going to build in this article?
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 by Step Implementation
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.
XML
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/idRLView"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:orientation="horizontal"
android:weightSum="2">
<!--creating a counter fab for incrementing a count-->
<com.andremion.counterfab.CounterFab
android:id="@+id/fabOne"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="20dp"
android:layout_weight="1"
android:src="@drawable/ic_add"
android:tint="@color/purple_200"
app:backgroundTint="@color/white"
app:badgeBackgroundColor="@color/purple_200"
app:badgePosition="RightTop"
app:badgeTextColor="@color/white" />
<!--creating a counter fab for decrementing a count-->
<!--in this we are passing badge position as top right
badge text color as white
badge back ground color as green -->
<com.andremion.counterfab.CounterFab
android:id="@+id/fabTwo"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="20dp"
android:layout_weight="1"
android:src="@drawable/ic_sub_icon"
android:tint="@color/white"
app:backgroundTint="@color/white"
app:badgeBackgroundColor="@color/purple_200"
app:badgePosition="RightTop"
app:badgeTextColor="@color/white" />
</LinearLayout>
</RelativeLayout>
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.
Java
import android.os.Bundle;
import android.view.View;
import androidx.appcompat.app.AppCompatActivity;
import com.andremion.counterfab.CounterFab;
public class MainActivity extends AppCompatActivity {
CounterFab fabOne, fabTwo;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// initializing our variables.
fabOne = findViewById(R.id.fabOne);
fabTwo = findViewById(R.id.fabTwo);
// on below line we are setting default
// count as 10 for both our fab.
fabOne.setCount(10);
fabTwo.setCount(10);
// on below line we are adding click listener for fab one and fab two.
fabOne.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// on below line we are calling a
// method to increase the fab count.
fabOne.increase();
}
});
fabTwo.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// on below line we are calling a
// method to decrease the fab count.
fabTwo.decrease();
}
});
}
}
Now run your app and see the output of the app.
Output:
Similar Reads
Android Architecture Android architecture contains a different number of components to support any Android device's needs. Android software contains an open-source Linux Kernel having a collection of a number of C/C++ libraries which are exposed through application framework services. Among all the components Linux Kern
5 min read
Android Tutorial In this Android Tutorial, we cover both basic and advanced concepts. So whether you are a fresher (graduate) or an experienced candidate with several years of Android Development experience, you can follow this Android tutorial to kick-start your journey in Android app development. Our Android Tutor
15+ min read
Activity Lifecycle in Android with Demo App In Android, an activity is referred to as one screen in an application. It is very similar to a single window of any desktop application. An Android app consists of one or more screens or activities. Each activity goes through various stages or a lifecycle and is managed by activity stacks. So when
9 min read
Introduction to Android Development Android operating system is the largest installed base among various mobile platforms across the globe. Hundreds of millions of mobile devices are powered by Android in more than 190 countries of the world. It conquered around 71% of the global market share by the end of 2021, and this trend is grow
5 min read
Top 50 Android Interview Questions and Answers - SDE I to SDE III A Linux-based open-source OS, Android was created by Andy Rubin and became one of the most popular smartphone operating systems. With 71 percent of the market share worldwide, Android is on top. Because it is on top in the smartphone OS, Android development is always in demand.If you are seeking a j
15+ min read
Android UI Layouts Layouts in Android define the user interface and hold UI controls or widgets that appear on the screen of an application. Every Android application consists of View and ViewGroup elements. Since an application contains multiple activitiesâeach representing a separate screenâevery activity has multip
5 min read
Top 50 Flutter Interview Questions and Answers for 2025 Flutter is an open-source, cross-platform application development framework. It was developed by Google in 2017. It is used to build applications for Android, iOS, Linux, Mac, Windows, and the web. Flutter uses the Dart programming language. It provides a simple, powerful, efficient, and easy-to-und
15+ min read
Components of an Android Application There are some necessary building blocks that an Android application consists of. These loosely coupled components are bound by the application manifest file which contains the description of each component and how they interact. The manifest file also contains the appâs metadata, its hardware confi
3 min read
Android Studio Tutorial It is stated that "If you give me six hours to chop down a tree then I will spend the first four hours in sharpening the axe". So in the Android Development World if we consider Android Development as the tree then Android Studio should be the axe. Yes, if you are starting Android Development then y
9 min read
MVVM (Model View ViewModel) Architecture Pattern in Android Developers always prefer clean and structured code for projects. Organizing the codes according to a design pattern helps in the maintenance of the software. By having knowledge of all crucial logic parts of the android application, it is easier to add and remove app features. Further, design patter
8 min read