how to code Notification with different styles in Android

In this blog post, I would like to show how to code Notification with different styles in Android.

There are 3 styles for Notification.

  1. Normal View Notification
  2. Big View Notification
  3. Custom View Notification

 

In this blog, i’ll show you how to create and update a Normal View Notification.

1. Create a new project File -> Android Project. On activity_main.xml copy paste this code:


<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
 android:paddingLeft="@dimen/activity_horizontal_margin"
 android:paddingRight="@dimen/activity_horizontal_margin"
 android:paddingTop="@dimen/activity_vertical_margin"
 tools:context="gallery.geeks.notification.MainActivity" >

<LinearLayout
 android:layout_width="fill_parent"
 android:layout_height="fill_parent"
 android:orientation="vertical" >

<Button
 android:id="@+id/start"
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 android:text="start notification" />

<Button
 android:id="@+id/cancel"
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 android:text="cancel notification" />

<Button
 android:id="@+id/update"
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 android:text="update notification" />
 </LinearLayout>

</RelativeLayout>

 

2. Now create new xml file called layout_notification.xml layout and copy paste this code:


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:orientation="vertical" >

<TextView
 android:layout_width="fill_parent"
 android:layout_height="400dp"
 android:text="Hi, Your Detailed notification view goes here...." />

</LinearLayout>

 

3. Now open MainActivity.java and copy paste this code:


package gallery.geeks.notification;

import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;
import android.support.v4.app.TaskStackBuilder;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;

public class MainActivity extends Activity {
 private NotificationManager mNotificationManager;
 NotificationCompat.Builder mBuilder;
 private int notificationID = 100;
 private int numMessages = 0;

 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);

 Button startBtn = (Button) findViewById(R.id.start);
 startBtn.setOnClickListener(new View.OnClickListener() {
 public void onClick(View view) {
 displayNotification();
 }
 });

 Button cancelBtn = (Button) findViewById(R.id.cancel);
 cancelBtn.setOnClickListener(new View.OnClickListener() {
 public void onClick(View view) {
 cancelNotification();
 }
 });

 Button updateBtn = (Button) findViewById(R.id.update);
 updateBtn.setOnClickListener(new View.OnClickListener() {
 public void onClick(View view) {
 updateNotification();
 }
 });
 }

 protected void displayNotification() {
 Log.i("Start", "notification");
 Intent resultIntent = new Intent(this, NotificationView.class);

 TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
 stackBuilder.addParentStack(NotificationView.class);
 stackBuilder.addNextIntent(resultIntent);

 PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0,
 PendingIntent.FLAG_UPDATE_CURRENT);

 mBuilder = new NotificationCompat.Builder(this)
 .setSmallIcon(R.drawable.ic_launcher)
 .setContentTitle("New Message")
 .setContentText("You've received new message")
 .setTicker("New Message Alert!")
 .setDefaults(Notification.DEFAULT_SOUND).setAutoCancel(true);

 mBuilder.setNumber(++numMessages);

 mBuilder.setContentIntent(resultPendingIntent);
 mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
 mNotificationManager.notify(notificationID, mBuilder.build());

 }

 protected void cancelNotification() {
 Log.i("Cancel", "notification");
 mNotificationManager.cancel(notificationID);
 }

 protected void updateNotification() {
 Log.i("Update", "notification");
 Intent resultIntent = new Intent(this, NotificationView.class);

 TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
 stackBuilder.addParentStack(NotificationView.class);
 stackBuilder.addNextIntent(resultIntent);

 PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0,
 PendingIntent.FLAG_UPDATE_CURRENT);

 mBuilder = new NotificationCompat.Builder(this)
 .setSmallIcon(R.drawable.ic_launcher)
 .setContentTitle("Updated Message")
 .setContentText("You've got updated message.")
 .setTicker("Updated Message Alert!")
 .setDefaults(Notification.DEFAULT_SOUND).setAutoCancel(true);
 mBuilder.setNumber(++numMessages);

 mBuilder.setContentIntent(resultPendingIntent);
 mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
 mNotificationManager.notify(notificationID, mBuilder.build());
 }

}

4. Create a new class NotificationView.java and copy paste this code:

import android.app.Activity;
import android.os.Bundle;

public class NotificationView extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_notification);
}

}

5. Now open your AndroidManifest.xml file and copy paste this code:


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
 package="gallery.geeks.notification"
 android:versionCode="1"
 android:versionName="1.0" >

 <uses-sdk
 android:minSdkVersion="16"
 android:targetSdkVersion="19" />

 <application
 android:allowBackup="true"
 android:icon="@drawable/ic_launcher"
 android:label="@string/app_name"
 android:theme="@style/AppTheme" >
 <activity
 android:name=".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=".NotificationView"
 android:label="Details of notification"
 android:parentActivityName=".MainActivity">
 <meta-data
 android:name="android.support.PARENT_ACTIVITY"
 android:value=".MainActivity"/>
 </activity>
 </application>

</manifest>

 


ScreenShots:Screenshot_2014-12-12-16-32-44 
Screenshot_2014-12-12-16-32-52 
Screenshot_2014-12-12-16-32-31 
Screenshot_2014-12-12-16-33-03 
Screenshot_2014-12-12-16-33-12 
Screenshot_2014-12-12-16-33-25

Hope this helpful. Your valuable comments are always welcomed. It will help to improve my post and understanding.
Download Source Code from here.

durga chiranjeevi

durga chiranjeevi

I'm currently working as Android developer at Energy Alternatives India. Interested in Application and Game development.
durga chiranjeevi

Leave a Reply

Your email address will not be published. Required fields are marked *