5

I'm trying to show notifications in my app along with a progress bar in it.

My app specs are that I'm targeting API 15 with minimum SDK set to 8. I'm using the following code to show notifications:

NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this);

mBuilder.setContentTitle("My Application")
        .setContentText("Downloading...")
        .setProgress(0, 0, true)
        .setSmallIcon(R.drawable.ic_launcher);

PendingIntent in = PendingIntent.getActivity(getApplicationContext(), 0, getIntent(), 0);
mBuilder.setContentIntent(in);

mNotificationManager.notify(0, mBuilder.build());

With the above code the notifications appear in the notification drawer but no progress bar (on Android 2.2, and 2.3.3). But the progress bar appears fine on Android 4.1. So its obvious that its a compatibility issue.

How do I code that my notification with an indefinite progress bar appear on the APIs that I'm targeting. I tried using Notification.Builder but this class is not available on my current specs.

Thanks in advance!

-Faraz Azhar

2 Answers 2

0

Have you tried using http://developer.android.com/reference/android/widget/RemoteViews.html ?

3
  • Yes I have actually. I was trying to avoid custom layout for notification and hoping there would be a way to make the default notification classes work throughout the APIs. I guess if its the only way then I'll have no choice but to go for it. Commented Oct 16, 2012 at 12:11
  • I guess you have no other choice. You can still check the OS version before creating the notification, and use the system API whenever possible Commented Oct 16, 2012 at 12:15
  • Hmm. Thanks. RemoteViews it is then I guess. Commented Oct 16, 2012 at 12:29
0

I encourage you to use Notification Compat 2. It has been created to handle this kind of problem.
If you want to use a custom RemoteViews for your notifications, have a look at my fork. I have made a pull request but it has not been integrated yet.

7
  • Looks nice but I avoid third-party libraries, just cuz of their licensing requirements. I don't really want to use custom layouts, I thought I'd stick with the default android graphics and layout. Commented Oct 16, 2012 at 12:03
  • The goal of libraries like this one or ActionBarSherlock is to help the developers. 99% of the time they have a very convenient licence. The default layout is indeed most likely what you want to use for a notification. Most of the time horrors are created with remoteViews.
    – Teovald
    Commented Oct 16, 2012 at 12:32
  • I just tried remoteviews and the immediate horror I noticed is that the textview I used in it doesn't appear properly on gingerbread and jellybean at the same time. My default application theme is android:Theme.Light. The problem it creates is that the text forecolor for gingerbread seems to blend into the notification drawer background, whereas it appears nicely on jellybean. How'd I tackle with that. Hard coding again? Commented Oct 16, 2012 at 12:58
  • Actually I just thought of something : when did progress bar appears in notifications ? I may be wrong but I think they were not present until Honeycomb (3.0). If that is the case, a remoteViews would be your only option if you absolutely want to show a progressbar in a notification. But if it was not part of android 2.x, a dialog is fine.
    – Teovald
    Commented Oct 16, 2012 at 13:07
  • Progress bars have always been there in notifications. Simplest example being when you download apps from market, you see progress bar showing the progress of download. Commented Oct 16, 2012 at 13:10

Not the answer you're looking for? Browse other questions tagged or ask your own question.