How to Implement Notification Counter in Android?
Notification Counter basically counts the notifications that you got through an application and shows on the top of your application icon so that you get to know you get new messages or any new update without opening your application or specific feature like the message button in Instagram. Notification counter is a feature that is provided in almost all android applications nowadays for example Facebook, Whatsapp, Instagram, YouTube, Gmail these are some social apps that we use in our day-to-day life so that exactly the Notification counter feature does?
It’s used for various purposes like:
- To tell user that you got a new message
- To tell how many unread messages they have
- To notify users that a new feature or content is uploaded for example whenever your subscribed channel uploads a video on Youtube your Youtube notification count number increases.
What we are going to build in this article?
In this article, we are going to build a simple application that counts the number of notifications with the help of two buttons increment and decrement. Whenever the user clicks on the increment button the notification count increases and whenever the user clicks on the decrement button the notification count decreases along with these two buttons we are also going to implement or use a 3rd button that is a reset button that directly sets the notification count to the zero. A sample GIF is given below to get an idea about what we are going to do in this article. Note that we are going to implement this project using the Java language.
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: Create a new Vector asset for notification bell by following these steps. Right-click on drawable > new > vector asset > search for notification icon and select > finish. We are going to use this icon to show our notification count on top of it. Now, what exactly is the vector asset that we use in this step.
Vector Asset: Vector Asset is an Android Studio tool that allows you to add material icons and import Scalable Vector Graphic (SVG) and Adobe Photoshop Document (PSD) files as vector drawable resources to your project.
Note: You can use customize notification bell icon as well by selecting it from your device storage but here we are using by default notification icon that is provided by the android studio.
Image for reference:
Step 3: Working with the activity_main.xml file
In this step, we are going to design our three buttons named Increment , decrement and reset along with our notification bell in the activity_main.xml file with the help of vector asset that we create in step 2 and to show notification count we are going to use a textview and set that textview on the top of our bell icon that we are implementing using image view also we use an ImageView to show the image of gfg logo.
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.
In this step, we are going to implement our methods to perform operations like increment, decrement, reset. To do this create a new java class by doing following the steps. Right-click on project name > New > class > finish. Below is the code for the Notificationcount.java file.
Step 5: Working with the MainActivity.java file
In this final step we are going to implement onclick listeners to our three buttons named increment, decrement, reset in our MainActivity.java file and call functions increment, decrement, reset that we created in the previous step. Below is the code for the MainActivity.java file. Comments are added inside the code to understand the code in more detail.
Project Link: Click Here