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="http://schemas.android.com/apk/res/android"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:orientation="horizontal">
<ImageView
   android:id="@+id/imgview"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:contentDescription="notification icon"
  
   />    
<TextView
   android:id="@+id/txt"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:gravity="top"
   android:textColor="#1111ff"
   android:maxLines="5"
   android:singleLine="false"
  
   />  

</LinearLayout>


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.support.v4.app.Fragment;
import android.support.v7.app.ActionBarActivity;
import android.app.Notification;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.RemoteViews;
import android.widget.Toast;
import android.support.v4.app.NotificationCompat;
import android.app.NotificationManager;
public class MainActivity extends ActionBarActivity {

   Fragment fragment;
   private final int MY_NOTIFICATION_ID=100;
NotificationManager notificationManager;
   Notification myNotification;
   protected void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.activity_main);
     Intent intent=getIntent();
Bundle b=intent.getExtras();
if(b!=null)
Toast.makeText(this, b.getString("notifcontent") ,Toast.LENGTH_SHORT).show();
     makeNotification();
   }
  
  
   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(R.id.txt,"This is a custom notificaiton. It can display multiple lines of content.");
   // Set image to ImageView
   remoteview.setImageViewResource(R.id.imgview, R.drawable.edoclogo);
  
   myNotification=new NotificationCompat.Builder(this)
      .setContent(remoteview)
      //.setContentTitle("Edoc")
      .setContentIntent(contentIntent)
      //.setTicker("Notification!")
      .setWhen(System.currentTimeMillis())
      .setSound(Uri.parse("android.resource://com.example.androidexample/"+R.raw.notsound))
      .setAutoCancel(true)
      .setSmallIcon(R.drawable.ic_launcher)     
      .build();
       notificationManager = (NotificationManager)this.getSystemService(Context.NOTIFICATION_SERVICE);
       notificationManager.notify(MY_NOTIFICATION_ID, myNotification);       

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

   @Override
   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 == R.id.action_settings) {
        return true;
     }
     return super.onOptionsItemSelected(item);
   }
}

Android-custom-notification

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.