Saturday, October 5, 2019

Android Progress Notification with Examples

In android, Progress Notification is used to show the progress of an ongoing operation in notification bar. By using progress notification, we can easily know that how much percentage of the current operation completed and how long the operation will run to complete the remaining operation.

In android, two types of progress indicators available, one is determinate and another one is indeterminate. If we are known about how long the operation will take to complete, then we can use determinate form of the indicator. In case if we are not aware of how long the operation will run then we can use indeterminate form of indicator.

Following is the pictorial representation of using different type of android progress indicators (determinate and indeterminate) to show the progress of current operation in notification.

Android Progress Notification in Determinate and Indeterminate Modes

Generally, the progress indicators in android are implemented by using ProgressBar class. To display the progress indicators in our app, we need to add the progress bar to our notification by calling setProgress(max, progress, false) method and then issue the notification.

Here the third argument in setProgress() method is used to indicate whether the progress bar is determinate (false) or indeterminate (true). As our operation proceeds, we need to increase the value of progress, and update the status of notification. At the end of operation, the progress value must be equal to max value. The better way to call setProgress() is to set max value to 100 and then increment progress as a percent complete value for the operation.

Once the operation is done leave the progress bar showing or remove it by calling setProgress(0,0, false) and update the notification text to show that the operation is complete.

Now we will see how to create and show the progress bar in android notification bar with examples.

Android Progress Notification Example

Create a new android application using android studio and give names as ProgressNotification. In case if you are not aware of creating an app in android studio check this article Android Hello World App.

Now open an activity_main.xml file from \res\layout path and write the code like as shown below

activity_main.xml

<?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="vertical" >
    <
Button
        
android:id="@+id/btnShow"
        
android:layout_width="wrap_content"
        
android:layout_height="wrap_content"
        
android:text="Show Notification"
        
android:layout_marginTop="100dp" android:layout_marginLeft="120dp"/>
</
LinearLayout>
If you observe above code we created a one Button control in XML Layout file to show the progress indicator in notification when we click on Button

Once we are done with creation of layout with required controls, we need to load the XML layout resource from our activity onCreate() callback method, for that open main activity file MainActivity.java from \java\com.tutlane.progressnotification path and write the code like as shown below.

MainActivity.java

package com.tutlane.progressnotification;import android.app.NotificationManager;import android.content.Context;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.support.v7.app.NotificationCompat;import android.util.Log;import android.view.View;import android.widget.Button;
public class MainActivity extends AppCompatActivity {
    
private NotificationManager mNotifyManager;
    
private NotificationCompat.Builder mBuilder;
    
int id 1;
    
@Override
    
protected void onCreate(Bundle savedInstanceState) {
        
super.onCreate(savedInstanceState);
        setContentView(R.layout.
activity_main);
        Button b1 = (Button) findViewById(R.id.
btnShow);
        b1.setOnClickListener(
new View.OnClickListener() {
            
@Override
            
public void onClick(View v) {
                
mNotifyManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
                
mBuilder new NotificationCompat.Builder(MainActivity.this);
                
mBuilder.setContentTitle("File Download")
                        .setContentText(
"Download in progress")
                        .setSmallIcon(R.drawable.
download);
                
// Start a the operation in a background thread
                
new Thread(
                        
new Runnable() {
                            
@Override
                            
public void run() {
                                
int incr;
                                
// Do the "lengthy" operation 20 times
                                
for (incr = 0; incr <= 100; incr+=5) {
                                    
// Sets the progress indicator to a max value, the current completion percentage and "determinate" state
                                    
mBuilder.setProgress(100, incr, false);
                                    
// Displays the progress bar for the first time.
                                    
mNotifyManager.notify(idmBuilder.build());
                                    
// Sleeps the thread, simulating an operation
                                    
try {
                                        
// Sleep for 1 second
                                        
Thread.sleep(1*1000);
                                    } 
catch (InterruptedException e) {
                                        Log.d(
"TAG""sleep failure");
                                    }
                                }
                                
// When the loop is finished, updates the notification
                                
mBuilder.setContentText("Download completed")
                                        
// Removes the progress bar
                                        
.setProgress(0,0,false);
                                
mNotifyManager.notify(idmBuilder.build());
                            }
                        }
                
// Starts the thread by calling the run() method in its Runnable
                
).start();
            }
        });
    }
}
If you observe above code we are created a progress notification using setProgress() method and showing the progress notification on Button click.

Generally, during the launch of our activityonCreate() callback method will be called by android framework to get the required layout for an activity.

Output of Android Progress Notification Example

When we run above example using android virtual device (AVD) we will get a result like as shown below.

Android Progress Notification Example Result

If you observe above result we created a progress notification and shown it on Button click based on our requirements.

This is how we can use progress notification in our android applications based on our requirements.

No comments:

Post a Comment

How to DROP SEQUENCE in Oracle?

  Oracle  DROP SEQUENCE   overview The  DROP SEQUENCE  the statement allows you to remove a sequence from the database. Here is the basic sy...