You are on page 1of 13

Home

About

Android Course Training

Android Developer Club

Subscribe:

Posts

Comments

Email

Android Tutorial | Android SDK


Development & Programming
Search in t

Advertise Here

Jelly Bean Notifications in Android


Posted by Sushant on March 5, 2013, filed in: ANDROID SDK TUTORIAL

X
Hello there! If you are new here, you might want to subscribe to the RSS feed for updates
on this topic.

This example shows how to use notifications in android jelly bean


version. You may have heard about Android Jelly Bean (API level 16). Google has improved
a lot of features and introduced new features. One of them is the notification. Now they have
made the notification more versatile by introducing media rich notification

Algorithm:

1.) Create a new project by File-> New -> Android Project name it JellyBeanNotifications.

2.) Write following into main.xml:

1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
2
android:layout_width="match_parent"
3 android:layout_height="match_parent"
4 android:orientation="vertical"
5 android:gravity="center_horizontal">
6
7 <Button
android:id="@+id/btBasicNotification"
8 android:layout_width="fill_parent"
9 android:layout_height="wrap_content"
10 android:gravity="center_horizontal|center_vertical"
11 android:onClick="sendBasicNotification"
12 android:text="@string/btBasicNotification"
android:background="@drawable/button_background"
13 android:textColor="#000000"
14 />
15 <Button
16 android:id="@+id/btBigTextNotification"
android:layout_width="fill_parent"
17 android:layout_height="wrap_content"
18 android:gravity="center_horizontal|center_vertical"
19 android:onClick="sendBigTextStyleNotification"
20 android:text="@string/btBigTextNotification"
21 android:background="@drawable/button_background"
android:textColor="#000000"
22 />
23 <Button
24 android:id="@+id/btBigPictureNotification"
25 android:layout_width="fill_parent"
26 android:layout_height="wrap_content"
android:gravity="center_horizontal|center_vertical"
27 android:onClick="sendBigPictureStyleNotification"
28 android:text="@string/btBigPictureNotification"
29 android:background="@drawable/button_background"
30
31
32
33
34 android:textColor="#000000" />
<Button
35 android:id="@+id/btInboxStyleNotification"
36 android:layout_width="fill_parent"
37 android:layout_height="wrap_content"
38 android:gravity="center_horizontal|center_vertical"
39 android:onClick="sendInboxStyleNotification"
android:text="@string/btInboxStyleNotification"
40 android:background="@drawable/button_background"
41 android:textColor="#000000"/>
42</LinearLayout>
43
44
45
46

3.) Write following into strings.xml:

1 <?xml version="1.0" encoding="utf-8"?>


2 <resources>
3
4 <string name="app_name">JellyBeanNotifications</string>
<string name="hello_world">Hello world!</string>
5 <string name="menu_settings">Settings</string>
6 <string name="btBasicNotification">Send Basic Notification</string>
7 <string name="btBigTextNotification">Send Big Text Style
8 Notification</string>
9 <string name="btBigPictureNotification">Send Big Picture Style
Notification</string>
1 <string name="btInboxStyleNotification">Send Inbox Style
0 Notification</string>
11 <string name="tvHandleNotification">You can handle notification
1 intent here.</string>
</resources>
2

4.) Create and write following into values/dimens.xml:

1 <resources>
2
3 <dimen name="padding_small">8dp</dimen>
4 <dimen name="padding_medium">8dp</dimen>
5 <dimen name="padding_large">16dp</dimen>
6
</resources>
7

5.) Write following into manifest file:

1 <?xml version="1.0" encoding="utf-8"?>


2 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.jellybeannotifications"
3 android:versionCode="1"
4 android:versionName="1.0" >
5
<uses-sdk
6
android:minSdkVersion="16"
7 android:targetSdkVersion="16" />
8
9 <application
1 android:allowBackup="true"
0 android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
1 android:theme="@style/AppTheme" >
1 <activity
1 android:name="com.example.jellybeannotifications.JellyBeanNot
2 ificationsActivity"
1 android:label="@string/app_name" >
<intent-filter>
3 <action android:name="android.intent.action.MAIN" />
1
4 <category android:name="android.intent.category.LAUNCHER"
1 />
5 </intent-filter>
</activity>
1
6
<activity
1 android:name=".HandleNotificationActivity"
7 android:label="HandleNotificationActivity" >
1 <intent-filter>
8 <category android:name="android.intent.category.LAUNCHER"
/>
1 </intent-filter>
9 </activity>
2 </application>
0
2 </manifest>
1
2
2
2
3
2
4
2
5
2
6
2
7
2
8
2
9
3
0
3
1
3
2
3
3
3
4
3
5

6.) Create and write following into drawable/button_background.xml:

1<?xml version="1.0" encoding="utf-8"?>


2<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle"
3 android:id="@+id/bgButton">
4 <gradient android:startColor="#FFFFFFFF" android:endColor="#FFF0F0F0"
5android:angle="45"/>
6 <stroke android:width="2dp" android:color="#FFAAAAAA"/>
<padding android:left="4dp" android:top="1dp" android:right="4dp"
7android:bottom="1dp" />
8 <corners android:radius="7dp" />
9</shape>

7.) Create and write following into layout/handle_notification_activity.xml:

1
2 <?xml version="1.0" encoding="utf-8"?>
3 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
4 android:layout_width="match_parent"
android:layout_height="match_parent"
5 android:orientation="vertical"
6 android:gravity="center_horizontal|center_vertical" >
7
8 <TextView
9 android:id="@+id/textView1"
android:layout_width="wrap_content"
10 android:layout_height="wrap_content"
11 android:text="@string/tvHandleNotification"
12 android:textSize="20dp"
13 android:textStyle="bold|italic" />
14
15</LinearLayout>
16

8.) Create and write following into src/HandleNotificationActivity.java:

1 package com.example.jellybeannotifications;
2
import android.app.Activity;
3 import android.os.Bundle;
4
5 public class HandleNotificationActivity extends Activity {
6
7 @Override
8 protected void onCreate(Bundle savedInstanceState) {
9
10 super.onCreate(savedInstanceState);
setContentView(R.layout.handle_notification_activity);
11 }
12 }
13

9.) Add ic_launcher_share.png, ic_launcher_web.png and big_picture.png into drawable


folder.
10.) Run for output.

Steps:

1.) Create a project named JellyBeanNotifications and set the information as stated in the
image.

Build Target: Android 4.0


Application Name: JellyBeanNotifications
Package Name: com. example. JellyBeanNotifications
Activity Name: JellyBeanNotifications
Min SDK Version: 4.0

2.) Open JellyBeanNotifications.java file and write following code there:

1 package com.example.jellybeannotifications;
2
import android.app.Activity;
3
import android.app.Notification;
4 import android.app.Notification.Builder;
5 import android.app.NotificationManager;
6 import android.app.PendingIntent;
import android.content.Intent;
7
import android.graphics.BitmapFactory;
8 import android.os.Bundle;
9 import android.view.Menu;
10 import android.view.View;
11
12 public class JellyBeanNotificationsActivity extends Activity {
13
@Override
14 public void onCreate(Bundle savedInstanceState) {
15 super.onCreate(savedInstanceState);
16 setContentView(R.layout.main);
17 }
18
19 @Override
public boolean onCreateOptionsMenu(Menu menu) {
20 getMenuInflater().inflate(R.menu.main, menu);
21 return true;
22 }
23
24 public void sendBasicNotification(View view) {
Notification notification = new Notification.Builder(this)
25 .setContentTitle("Basic Notification")
26 .setContentText("Basic Notification, used earlier")
27 .setSmallIcon(R.drawable.ic_launcher_share).build();
28 notification.flags |= Notification.FLAG_AUTO_CANCEL;
29 NotificationManager notificationManager =
getNotificationManager();
30 notificationManager.notify(0, notification);
31 }
32
33 public void sendBigTextStyleNotification(View view) {
34 String msgText = "Jeally Bean Notification example!! "
35 + "where you will see three different kind of
notification. "
36 + "you can even put the very long string here.";
37
38 NotificationManager notificationManager =
39 getNotificationManager();
40 PendingIntent pi = getPendingIntent();
Builder builder = new Notification.Builder(this);
41 builder.setContentTitle("Big text Notofication")
42 .setContentText("Big text Notification")
43 .setSmallIcon(R.drawable.ic_launcher)
44 .addAction(R.drawable.ic_launcher_web, "show activity",
45 pi)
.setAutoCancel(true);
46 Notification notification = new
47 Notification.BigTextStyle(builder)
48 .bigText(msgText)
49 .build();
notificationManager.notify(0, notification);
50
}
51
52 public void sendBigPictureStyleNotification(View view) {
53 PendingIntent pi = getPendingIntent();
54 Builder builder = new Notification.Builder(this);
55 builder.setContentTitle("BP notification")
56 // Notification title
.setContentText("BigPicutre notification")
57
// you can put subject line.
58 .setSmallIcon(R.drawable.ic_launcher)
59 // Set your notification icon here.
60 .addAction(R.drawable.ic_launcher_web, "show activity",
61 pi)
.addAction(
62 R.drawable.ic_launcher_share,
63 "Share",
64 PendingIntent.getActivity(getApplicationContext(
65 ), 0,
66 getIntent(), 0, null));
67
// Now create the Big picture notification.
68 Notification notification = new
69 Notification.BigPictureStyle(builder)
70 .bigPicture(
71 BitmapFactory.decodeResource(getResources(),
R.drawable.big_picture)).build();
72 // Put the auto cancel notification flag
73 notification.flags |= Notification.FLAG_AUTO_CANCEL;
74 NotificationManager notificationManager =
75 getNotificationManager();
76 notificationManager.notify(0, notification);
}
77
78 public void sendInboxStyleNotification(View view) {
79 PendingIntent pi = getPendingIntent();
80 Builder builder = new Notification.Builder(this)
81 .setContentTitle("IS Notification")
82 .setContentText("Inbox Style notification!!")
.setSmallIcon(R.drawable.ic_launcher)
83 .addAction(R.drawable.ic_launcher_web, "show activity",
84 pi);
85
86 Notification notification = new Notification.InboxStyle(builder)
87 .addLine("First message").addLine("Second message")
.addLine("Thrid message").addLine("Fourth Message")
88 .setSummaryText("+2 more").build();
89 // Put the auto cancel notification flag
90 notification.flags |= Notification.FLAG_AUTO_CANCEL;
91 NotificationManager notificationManager =
92 getNotificationManager();
notificationManager.notify(0, notification);
93 }
94
95 public PendingIntent getPendingIntent() {
96 return PendingIntent.getActivity(this, 0, new Intent(this,
97 HandleNotificationActivity.class), 0);
}
98
99
public NotificationManager getNotificationManager() {
10 return (NotificationManager)
0 getSystemService(NOTIFICATION_SERVICE);
10 }
1 }
10
2
10
3
10
4
10
5
10
6
10
7
10
8
10
9
110

3.) Compile and build the project.

Output
Tags: Jelly Bean Android, Jelly Bean Notifications Android, Jelly Bean Notifications
example Android, Jelly Bean Notifications in Android, Jelly Bean Notifications tutorial
Android

Previous

Multi Touch Demo in Android

Next

Splitting Touch Example in Android

Leave a Reply

Name (Required)

Mail (will not be published) (Required)


Website

Notify me of followup comments via e-mail

Buzz

Twitter

Facebook

RSS

Email

About Author

Advertise HereAdvertise HereAdvertise HereAdvertise Here

Sign Up For Your Free Android Course Material for 3 Weeks including Free Videos and Source
Code.

Name:

Email:
2013 Android Tutorial | Android SDK Development & Programming. All rights reserved.
Proudly designed by Theme Junkie.

You might also like