Show and Clear Android Notification Example

A notification is a message that you can display to the users outside of your android application UI. If you tell the system to display notification to the users, they will see an icon in the notification bar to view the details of the notification, they need to scroll down the notification bar to open notification drawer. You can display simple notification, custom notification, push notification and so on. In this tutorial, you will learn to create, display and also clear simple notification in your android application.

Notification is send to the users to give some information about application, updates, offer and so on. Email application notify you if there is any new mail received and news app show notifications to the user if there is any viral news in the network.

Android User Interface (UI) Design Tutorial
Android Application Development Tutorial
YouTube Android API Integration: Getting Started with YouTube Android API

Android Example: How to Show and Clear Notification in Android App

Let’s create a new android application with the project name: Android Notification Example, domain name: viralandroid.com, choose API 11 as minimum API level and blank activity.

XML Layout File

Open your XML layout file and add two buttons one to show notification and another to clear your notification with different id inside any layout. Following is the complete content of XML layout file.


Java Activity File

Open your java activity file and declare the variables for Button, NotificationManager, Notification and PendingIntent. Override onClick listener for both button to show and clear notification in android. Following is the complete java code of activity file.


Android Example: How to Show and Clear Notification in Android App

Now, run your Show and Clear Android Notification Example application and click on Show Notification Button, you will see notification icon in notification bar. If you open notification drawer you will see a notification with icon, short description and sub title that we have created above. To clear this notification you need to click on the Clear Notification button. Above, there is a screenshot of this android notification example project.
Tutorial 7602129215086702388
Home item