how to code Notification with different styles in Android – part III
In this blog post, I would like to show how to code Notification with different styles in Android.
There are 3 styles for Notification.
- Normal View Notification
- Big View Notification
- Custom View Notification
In this blog, i’ll show you how to create a Custom 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" > <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:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_marginTop="72dp" android:text="cancel notification" /> </RelativeLayout>
2. Create a new layout, notification_layout.xml and copy paste this code:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <ImageView android:id="@+id/imageView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_alignParentTop="true" android:layout_marginTop="19dp" android:src="@drawable/ic_launcher" /> <ProgressBar android:id="@+id/progressBar1" style="?android:attr/progressBarStyleHorizontal" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentRight="true" android:layout_alignParentTop="true" /> <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignTop="@+id/imageView1" android:layout_toLeftOf="@+id/imageView1" android:text="Small Text" android:textAppearance="?android:attr/textAppearanceSmall" /> </RelativeLayout>
3. Now open your MainActivity.java and copy paste this code:
package com.example.customviewnotification; import android.app.Activity; import android.app.Notification; 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.os.Build; import android.os.Bundle; import android.support.v4.app.NotificationCompat; import android.util.Log; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.Button; import android.widget.RemoteViews; 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); } </pre> <pre>Intent resultIntent = new Intent(this, NotificationView.class); mBuilder = new NotificationCompat.Builder(this) .setSmallIcon(R.drawable.ic_launcher) .setAutoCancel(true) .setContentText("hello world!") .setContentTitle("Android"); TaskStackBuilder stackBuilder = TaskStackBuilder.create(this); stackBuilder.addParentStack(NotificationView.class); stackBuilder.addNextIntent(resultIntent); PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT); mBuilder.setContentIntent(resultPendingIntent); Notification notification = null; if (Build.VERSION.SDK_INT > 15) { notification = buildForJellyBean(mBuilder); } else { notification = mBuilder.build(); }</pre> <pre> private Notification buildForJellyBean(NotificationCompat.Builder builder1) { // TODO Auto-generated method stub builder1.setPriority(Notification.PRIORITY_HIGH); return builder1.build(); } }
ScreenShot:
Hope this helpful. Your valuable comments are always welcomed. It will help to improve my post and understanding.
Download Source Code from here.
Related Posts
durga chiranjeevi
Latest posts by durga chiranjeevi (see all)
- Sharing Application Data Between Two Android Apps – Part III - April 8, 2015
- Sharing Application Data Between Two Android Apps – Part II - April 8, 2015
- Sharing Application Data Between Two Android Apps – Part I - April 8, 2015