Thursday 13 October 2011

Custom Notification

Setting the Custom Notification in Notification Bar.

Required a custom XML Layout for setting the custom notification.

Following is the XML layout file. (res/layout/custom_notify).


<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:background="#FFFFFF">
  
  <ImageView android:id="@+id/notify_image"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:background="@drawable/correct_image"
  android:layout_margin="10dp">
  </ImageView>
  
  <RelativeLayout android:id="@+id/content_layout"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:layout_marginTop="10dp"
  android:layout_toRightOf="@+id/notify_image">
 
  <TextView android:id="@+id/title_text"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:text="Title Content"
  android:textStyle="bold"
  android:textSize="15sp"
  android:textColor="#000000">
  </TextView>
 
  <TextView android:id="@+id/message_text"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:layout_below="@+id/title_text"
  android:layout_marginTop="3dp"
  android:text="Message Content"
  android:textSize="15sp"
  android:textColor="#000000">
  </TextView>
 
  </RelativeLayout>
  
</RelativeLayout>


I have used one Imageview and two TextView in the custom notofication layout.


You have to use RemoteView for setting the custom notification in the notification bar.

Following is the code for setting custom notification in the notification bar.


package com.sample.notification;


import java.util.Calendar;


import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.Vibrator;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RemoteViews;
import android.widget.RemoteViews.RemoteView;


public class Test extends Activity
{
private Button button1;
private static final int NOTIFICAION_ID = 1;

    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        button1 = (Button) findViewById(R.id.button01);
        button1.setOnClickListener(button1Listner);
        
        onPush("Notification 2","Channel 2");
    }
    
public void onPush(String notification, String channel) 
{
customNotification(notification, channel); // For Custom Notification
}

public void customNotification(String title, String message)
{
NotificationManager notfManager = (NotificationManager) 
                       getSystemService(NOTIFICATION_SERVICE);

CharSequence tickerText  = "Custom Notificaions";
long when = System.currentTimeMillis();
int icon = R.drawable.notf_icon;
Notification notification = new Notification(icon, tickerText, when);

RemoteViews contentView = new RemoteViews(getPackageName(),  
                                                       R.layout.custom_notify);
contentView.setImageViewResource(R.id.notify_image,
                                                            R.drawable.correct_image);
contentView.setTextViewText(R.id.title_text, title);
contentView.setTextViewText(R.id.message_text, message);
notification.contentView = contentView;

Intent notificationIntent=new Intent(this, Notification.class);
PendingIntent pendingContentIntent=PendingIntent.getActivity(this, 
                                                                                  0, notificationIntent, 0);
notification.contentIntent=pendingContentIntent;

notfManager.notify(NOTIFICAION_ID, notification);
}

private OnClickListener button1Listner = new View.OnClickListener() {

public void onClick(View arg0)
{
startActivity(new Intent("com.sample.notification.Notificaions"));
}
};
}


Following is the Screen shot for Custom Notification.





You can also add the vibration and LED light color Display like the default notification.

Refer this link for below link for default notification, adding vibration and LED light color change.
http://tjkannan.blogspot.com/2011/10/default-notification.html


No comments:

Post a Comment