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

Faraz Azhar
  • 562
  • 9
  • 28

2 Answers2

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.

Teovald
  • 4,369
  • 4
  • 26
  • 45
  • 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. – Faraz Azhar Oct 16 '12 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 Oct 16 '12 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? – Faraz Azhar Oct 16 '12 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 Oct 16 '12 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. – Faraz Azhar Oct 16 '12 at 13:10
  • Anyway I think I found a solution to text color problems [on this website](http://www.framentos.com/en/android-tutorial/2012/02/20/how-to-create-a-custom-notification-on-android/) – Faraz Azhar Oct 16 '12 at 13:13
  • Nice, I was not so sure of it without a reference is the api :-). I have been using 4.x for quite some time now. So yes, detecting the version and coloring the text if it is too low to follow your style is probably the best way to deal with this issue. – Teovald Oct 16 '12 at 13:30
0

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

parser_failed
  • 697
  • 9
  • 20
  • 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. – Faraz Azhar Oct 16 '12 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 – parser_failed Oct 16 '12 at 12:15
  • Hmm. Thanks. RemoteViews it is then I guess. – Faraz Azhar Oct 16 '12 at 12:29