Android custom notification

When you build an application that displays the notification to the user, you might want to customize the notification to use different icon, text color, and to display multiple lines of text. To achieve this goal, you need to use a custom notification layout that defines the appearance of the notification in the RemoteViews object. You can define your notification's icon and text by using the methods of the RemoteViews object. To apply the RemoteViews object to the notification, you need to put it into the NotificationCompat.Builder by calling the setContent() method.

Now to have a workable example application on custom notification, create a new Android project named androidexample. Then in the res/layout folder, you create a custom notification layout file called custom_notification_layout.xml file as shown below. In the custom layout file, there are one ImageView, and one TextView. The ImageView displays the icon image and the TextView displays the multiple lines of text of the notification. The TextView is able to expand the text to multiple lines because i assign the false value to the android:singleLine
attribute. The maximum lines that it can show are five. The icon image and text will be set using the methods of the RemoteViews. You will see the code in the MainActivity class below.

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

<LinearLayout xmlns:android=""
   android:contentDescription="notification icon"


Here is the content of the MainActivity class that you use RemoteViews to set icon and text to the notification, and display the notification to the user.

 package com.example.androidexample;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.RemoteViews;
import android.widget.Toast;
public class MainActivity extends ActionBarActivity {

   Fragment fragment;
   private final int MY_NOTIFICATION_ID=100;
NotificationManager notificationManager;
   Notification myNotification;
   protected void onCreate(Bundle savedInstanceState) {
     Intent intent=getIntent();
Bundle b=intent.getExtras();
Toast.makeText(this, b.getString("notifcontent") ,Toast.LENGTH_SHORT).show();
   public void makeNotification(){

   Intent notificationIntent = new Intent(this, MainActivity.class);
   notificationIntent .setAction("actionstring" + System.currentTimeMillis());
   notificationIntent.putExtra("notifcontent", "Hello World");
   PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
   // Create RemoteViews that inflates the custom layout file
   RemoteViews remoteview=new RemoteViews("com.example.androidexample",R.layout.custom_notification_layout);
   // Set text to TextView
   remoteview.setTextViewText(,"This is a custom notificaiton. It can display multiple lines of content.");
   // Set image to ImageView
   remoteview.setImageViewResource(, R.drawable.edoclogo);
   myNotification=new NotificationCompat.Builder(this)
       notificationManager = (NotificationManager)this.getSystemService(Context.NOTIFICATION_SERVICE);
       notificationManager.notify(MY_NOTIFICATION_ID, myNotification);       

   public boolean onCreateOptionsMenu(Menu menu) {
     // Inflate the menu; this adds items to the action bar if it is present.
     getMenuInflater().inflate(, menu);
     return true;

   public boolean onOptionsItemSelected(MenuItem item) {
     // Handle action bar item clicks here. The action bar will
     // automatically handle clicks on the Home/Up button, so long
     // as you specify a parent activity in AndroidManifest.xml.
     int id = item.getItemId();
     if (id == {
        return true;
     return super.onOptionsItemSelected(item);


Posted by: Dara | post date: 10-05-2014 | Subject: Android Apps Development

write programming tip

This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.