how to code Notification with different styles in Android – part II

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 a Big View Notification.

Big view Notification contains 3 sub styles:

  1. Big Picture Style
  2. Big Text Style
  3. Inbox Style

 

Big Picture Style:

On MainActivity.java class, copy paste this code:


package geeks.gallery.bigpicturenotification;

import android.app.Activity;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.TaskStackBuilder;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;
import android.util.Log;
import android.view.View;
import android.widget.Button;

public class MainActivity extends Activity {
private NotificationManager mNotificationManager;
private int notificationID = 100;
NotificationCompat.Builder mBuilder;
Bitmap remote_picture = null;

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();
}
});

}

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

protected void displayNotification() {
Log.i("Start", "notification");
Bitmap icon1 = BitmapFactory.decodeResource(getResources(),
R.drawable.ic_launcher);

mBuilder = new NotificationCompat.Builder(
this)
.setAutoCancel(true)
.setContentTitle("Big Picture Normal")
.setContentText("This is an example of a Big Picture Style.")
.setSmallIcon(R.drawable.ic_launcher).setLargeIcon(icon1);

NotificationCompat.BigPictureStyle bigPicStyle = new NotificationCompat.BigPictureStyle();
bigPicStyle.bigPicture(icon1);
bigPicStyle.setBigContentTitle("Big Picture Expanded");
bigPicStyle.setSummaryText("Nice big picture.");
mBuilder.setStyle(bigPicStyle);

Intent resultIntent = new Intent(this, NotificationView.class);

TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);

tackBuilder.addParentStack(NotificationView.class);

stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0,
PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(resultPendingIntent);

mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

mNotificationManager.notify(notificationID, mBuilder.build());
}

}

ScreenShot:

Screenshot_2014-12-13-15-06-41 Screenshot_2014-12-13-15-06-54 Screenshot_2014-12-13-15-07-03 Screenshot_2014-12-13-15-07-20

 


 

Big Text Style:

On MainActivity.java class, copy paste this code:


package com.example.bigtextnotificatoin;

import android.app.Activity;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.TaskStackBuilder;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;
import android.util.Log;
import android.view.View;
import android.widget.Button;

public class MainActivity extends Activity {
private NotificationManager mNotificationManager;
private int notificationID = 100;
NotificationCompat.Builder mBuilder;
Bitmap remote_picture = null;

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();
}
});

}

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

protected void displayNotification() {
Log.i("Start", "notification");
Bitmap icon1 = BitmapFactory.decodeResource(getResources(),
R.drawable.ic_launcher);

mBuilder = new NotificationCompat.Builder(this)
.setAutoCancel(true)
.setContentTitle("Big Text Normal")
.setContentText("This is an example of a Big Text Style.")
.setSmallIcon(R.drawable.ic_launcher).setLargeIcon(icon1);

NotificationCompat.BigTextStyle bigText = new NotificationCompat.BigTextStyle();
bigText.bigText("Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.");
bigText.setBigContentTitle("Big Text Expanded");
bigText.setSummaryText("Demo Content");
mBuilder.setStyle(bigText);

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.setContentIntent(resultPendingIntent);

mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

mNotificationManager.notify(notificationID, mBuilder.build());
}

}

 ScreenShot:

Screenshot_2014-12-13-15-07-32 Screenshot_2014-12-13-15-07-48 Screenshot_2014-12-13-15-07-57 Screenshot_2014-12-13-15-07-20


Inbox Style:

On MainActivity.java class, copy paste this code:


package com.example.inboxnotification;

import android.app.Activity;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;
import android.util.Log;
import android.view.View;
import android.widget.Button;

public class MainActivity extends Activity {
private NotificationManager mNotificationManager;
private int notificationID = 100;
NotificationCompat.Builder mBuilder;
Bitmap remote_picture = null;

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();
}
});

}

protected void displayNotification() {
 Log.i("Start", "notification");
 Bitmap icon1 = BitmapFactory.decodeResource(getResources(),
 R.drawable.ic_launcher);
 Intent resultIntent = new Intent(MainActivity.this, NotificationView.class);

 mBuilder = new NotificationCompat.Builder(
 this)
 .setAutoCancel(true)
 .setContentTitle("Inbox Style Normal")
 .setContentText("This is an example of a Inbox Style.")
 .setContentIntent(
 PendingIntent.getActivity(this, 0,
 resultIntent,
 PendingIntent.FLAG_ONE_SHOT))
 .setSmallIcon(R.drawable.ic_launcher).setLargeIcon(icon1);

 String[] events = new String[6]; 

 events[0] = "Message 1";
 events[1] = "Message 2";
 events[2] = "Message 3";
 events[3] = "Message 4";
 events[4] = "Message 5";
 events[5] = "Message 6"; 

 NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle(); 

 inboxStyle.setBigContentTitle("New Messages"); 

 for (int i = 0; i < events.length; i++) { 

 inboxStyle.addLine(events[i]);
 }
 mBuilder.setStyle(inboxStyle); 

 mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

 mNotificationManager.notify(notificationID, mBuilder.build());
 }

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

}



ScreenShot:

Screenshot_2014-12-13-15-09-12 Screenshot_2014-12-13-15-09-21 Screenshot_2014-12-13-15-09-28 Screenshot_2014-12-13-15-07-20

 

 


 

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 *