You must have seen various notifications in an Android phone for messages, mails, WhatsApp and so on. So in this article, you will learn how to create a simple notification.
A notification is a message you can display to the user outside of your application's normal UI. A notification appears as an icon in the notification area. Further to have more information, the user can open the notification drawer. Both the notification area and notification drawer are system-controlled; that is, they can be viewed by the user at any time.
Step 1
Open "activity_main" and add the following code to it:
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:paddingLeft="@dimen/activity_horizontal_margin"
- android:paddingRight="@dimen/activity_horizontal_margin"
- android:paddingTop="@dimen/activity_vertical_margin"
- android:paddingBottom="@dimen/activity_vertical_margin"
- tools:context=".MainActivity"
- android:background="#6b8e47">
- <Button
- android:id="@+id/button1"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:onClick="make"
- android:text="Create Notification"
- android:paddingLeft="10dp"
- android:paddingRight="10dp"
- android:layout_marginTop="150dp"
- android:layout_marginLeft="80dp"
- android:background="#cc9744">
- </Button>
- </LinearLayout>
In the code above, clicking on the button will create a notification.
The layout looks like:
Step 2
Create a layout file for showing the notification.
Right-click on layout then select "New" -> "Layout Resource file". Name this file "notification_layout" and add the following code to it:
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical"
- android:background="#454545">
- <TextView
- android:id="@+id/textView1"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="Hii..."
- android:textSize="30dp"
- android:layout_marginTop="50dp"
- android:layout_marginLeft="30dp"
- android:textColor="#FFFFFF"/>
- <TextView
- android:id="@+id/textView2"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="My first notification...."
- android:textSize="30dp"
- android:layout_marginLeft="30dp"
- android:layout_marginTop="50dp"
- android:textColor="#FFFFFF"/>
- </LinearLayout>
This layout file will be displayed when the user clicks on the notification.
The layout looks like:
Step 3
Open "MainActivity" and add the following code to it:
- package com.chhavi.notifications;
-
- import android.app.Notification;
- import android.app.NotificationManager;
- import android.app.PendingIntent;
- import android.content.Intent;
- import android.os.Bundle;
- import android.app.Activity;
- import android.view.View;
-
- public class MainActivity extends Activity {
-
- @Override
- protected void onCreate(Bundle savedInstanceState) {
-
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- }
-
- public void make(View view) {
-
- Intent intent = new Intent(this, DisplayNotification.class);
- PendingIntent pending = PendingIntent.getActivity(this, 0, intent, 0);
-
- Notification notifications = new Notification.Builder(this)
- .setContentTitle("New notification like message from")
- .setContentText("Hii").setSmallIcon(R.drawable.notification)
- .setContentIntent(pending)
- .addAction(R.drawable.reply, "Reply", pending)
- .addAction(R.drawable.cancel, "cancel", pending)
- .addAction(R.drawable.settings, "setings", pending).build();
-
- NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
- notifications.flags |= Notification.FLAG_AUTO_CANCEL;
- manager.notify(0, notifications);
-
- }
- }
According to the code above, the "DisplayNotification" activity will be loaded when the notification will be selected.
The image named "notification" will be displayed as an icon on the notification area when the notification is created.
On opening the notification drawer:
- "setContentTitle" will set the title of the notification.
- "setContentText" will be the second row of the notification description
- "addAction" is to add any other action that is required.
For example: for a calling notification, answer and disconnect are two actions. A notification can display up to 3 actions.
"Notification.FLAG_AUTO_CANCEL" will remove the notification from the notification area once it has been selected.
Step 4
Create a new Java class in the same package. Name it "DisplayNotification" and add the following code to it:
- package com.chhavi.notifications;
-
- import android.app.Activity;
- import android.os.Bundle;
-
- public class DisplayNotification extends Activity {
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.notification_layout);
- }
- }
Step 5
Open "AndroidManifest" and add the following code to it:
- <?xml version="1.0" encoding="utf-8"?>
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="com.chhavi.notifications"
- android:versionCode="1"
- android:versionName="1.0" >
-
- <uses-sdk
- android:minSdkVersion="16"
- android:targetSdkVersion="16" />
-
- <application
- android:allowBackup="true"
- android:icon="@drawable/ic_launcher"
- android:label="@string/app_name"
- android:theme="@style/AppTheme" >
- <activity
- android:name="com.chhavi.notifications.MainActivity"
- android:label="@string/app_name" >
- <intent-filter>
- <action android:name="android.intent.action.MAIN" />
- <category android:name="android.intent.category.LAUNCHER" />
- </intent-filter>
- </activity>
- <activity android:name=".DisplayNotification"
- android:label="Display Notification"/>
- </application>
- </manifest>
Note that minimum SDK version required by "Notification.Builder" is 11 and by "addAction" is 16.
Output snapshots:
Clicking on the button (Notification icon displayed):
Notification drawer pulled:
On selecting the notification:
Note that in the image above, the notification icon is no longer visible.
Thank you.