Skip to content
Related Articles

Related Articles

Improve Article
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: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:

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.




My Personal Notes arrow_drop_up
Recommended Articles
Page :