Introduction
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:





Mohamed KhanPosted Sep 8, 2016, 7:11 AM
Hi, I tried this example but i am finding errors as i am using the latest version of Android Studio.