Thursday, 13 October 2011

Custom Notification with Progress bar

Setting the Progress bar in the custom notification and display that custom notification in the notification bar.

You have to create a XML layout for custom notification.

Below is the XML layout file. (res/layout/custom_progress_notify.xml)


<?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/progress_notify_image"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:background="@drawable/globe_vista"
  android:layout_margin="10dp">
  </ImageView>
  
  <RelativeLayout android:id="@+id/progress_content_layout"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:layout_marginTop="10dp"
  android:layout_toRightOf="@+id/progress_notify_image">
 
  <TextView android:id="@+id/progress_title_text"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:text="Title Content"
  android:textStyle="bold"
  android:textSize="15sp"
  android:textColor="#000000">
  </TextView>
 
  <TextView android:id="@+id/progress_percentage"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_alignParentRight="true"
  android:layout_marginRight="5dp"
  android:text="100 / 100"
  android:textStyle="normal"
  android:textSize="15sp"
  android:textColor="#000000">
  </TextView>
 
  <ProgressBar android:id="@+id/progressBar1"
style="?android:attr/progressBarStyleHorizontal" 
android:layout_width="fill_parent"
android:layout_height="wrap_content" 
android:layout_below="@+id/progress_title_text"
android:layout_marginTop="3dp"
android:indeterminate="false"
android:max="10" 
android:padding="4dip">
</ProgressBar>
 
  </RelativeLayout>
  
</RelativeLayout>


I have added one Imageview, Two Textview and one Progress bar in the layout file.


For loading custom layout in the notification bar,  have to use RemoteView.


Thread and Handler class is used for updating the progress bar.
Thread is used for background process, and it is used to initialize the value of the progress.
You cant call the UI thread in another thread, Progress bar is updated only in the UI thread. For updating the progress bar using background thread, Handler class is used.

Below is the code for Custom Notification with Progress 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.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;

int progress=0;
Handler handler=null;

RemoteViews v=null;
Notification noti=null;
NotificationManager notiman=null;

    @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) 
{
customProgressNotification(notification, channel); // For Notification with progress bar
}

public void customProgressNotification(String title, String message)
{
handler=new Handler();
NotificationManager notfManager = (NotificationManager) 
                                           getSystemService(NOTIFICATION_SERVICE);

CharSequence tickerText  = "Custom Progress Notification";
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_progress_notify);
contentView.setImageViewResource(R.id.progress_notify_image, 
                                                   R.drawable.globe_vista);
contentView.setTextViewText(R.id.progress_title_text, title);
contentView.setProgressBar(R.id.progressBar1, 100, progress, false);
contentView.setTextViewText(R.id.progress_percentage, "0 / 100");
notification.contentView = contentView;

v=contentView;
noti=notification;
notiman=notfManager;

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

Runnable runnable=new Runnable(){


@Override
public void run() 
{
// TODO Auto-generated method stub
for(int i=0;i<=100;i++)
{
final int value=i;
Log.v("Test", "i Value=========>>>"+i);
try 
{
Thread.sleep(200);
}
catch (InterruptedException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
handler.post(new Runnable(){


@Override
public void run() 
{
// TODO Auto-generated method stub
noti.contentView.setProgressBar(R.id.progressBar1, 
                                                                                      100, value, false);
noti.contentView.setTextViewText(R.id.progress_percentage,
                                                                                              value+" / 100");
notiman.notify(NOTIFICAION_ID, noti);
}
});
}
}
};
new Thread(runnable).start();

}

private OnClickListener button1Listner = new View.OnClickListener() {

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


Below is the Screen shot.






No comments:

Post a Comment