tags:

views:

420

answers:

5

Hi,

The application I'm working on is a service which runs in the background in android. The problem is that I need to determine the height of the notification bar for some functionality of the service.

I found a number of solutions for this, for regular activities - a view inside an activity can determine its own height without the notification bar, based on checking its actual view size from its onSizeChanged event, after it is already drawn. However, this is not applicable to a service which has no physical view drawn.

I would really appreciate any ideas on getting the size of the notification bar at a system level, perhaps?

Thanks so much!

Vitaliy

A: 

Have an activity tell your service the proper size to use. The status bar height, nor the screen size, is likely to change during the operation of your service.

CommonsWare
Thanks for your response,But I have no specific activity that I can use to do it,and it seems wrong to create one especially for this.I believe that there should be some system level API to determine the height of the notification bar?To make things clearer - I don't care about a user "unwrapping" the notification bar, I would only like to know its default size - that is when the notification bar is folded..
Vitaliy
@Vitaly: Applications that do not have activities tend to be unpopular. Also, please do not assume that all Android devices will have the same screen configuration. Some might not have the status bar on top, for example.
CommonsWare
Thanks, CommonsWave. The application will be OK with such a configuration, as long as I'll be able to get the actual size of the notification bar..
Vitaliy
+1  A: 

Maybe you can retrieve an standard notification bar icon and measure it.

You'll have to use a system icon so you have more chances to be present on all Android customizations (Sense UI, MotoBlur, etc...)

Something like:

Drawable phoneCallIcon = getResources().getDrawable(android.R.drawable.stat_sys_phone_call);

int height = phoneCallIcon.getIntrinsicHeight();

Running this from my Nexus One gives 38 pixels height, which is accurate.


A safer approach to this 'hack' would be iterating through some of the standard notification icons.

It would be this way:

int notificationBarResources[] = {
            android.R.drawable.stat_sys_phone_call,
            android.R.drawable.stat_notify_call_mute,
            android.R.drawable.stat_notify_sdcard,
            android.R.drawable.stat_notify_sync,
            android.R.drawable.stat_notify_missed_call,
            android.R.drawable.stat_sys_headset,
            android.R.drawable.stat_sys_warning };
int notificationBarHeight = -1;
    for (int i = 0; i < notificationBarResources.length; i++) {
        try {
            Drawable phoneCallIcon = getResources().getDrawable(
                    android.R.drawable.stat_sys_phone_call);
            if ((notificationBarHeight = phoneCallIcon.getIntrinsicHeight()) != -1) {
                break;
            }
        } catch (Resources.NotFoundException e) {
            // Nothing to do
        }
    }
xamar
Thanks, xamar!That's a very interesting workaround!Is this specific icon guaranteed to be available in any version?I still wonder if there is no general way to get info about the notification bar? It seems such a basic thing..Many thanks for your help!Vitaliy
Vitaliy
I see their resource ids are exported (=public accessible) so this is unlikely to change.Anyway I would create an array with 5-8 notification bar icons and iterate trough them as long as 'Resources.NotFoundException'. This way you'll minimize the chances of failing.
xamar
A: 

Thanks xamar!

I'm still looking for a more general alternative, but in the meantime your workaround is the best one!

For some reason I can't mark it as an answer here..

Thanks again so much for your help!

I would still appreciate a more general approach, If someone could think of. I really can't believe that there is no API to get the notifications' bar height in Android..

Thanks! Vitaliy

A: 

I have the same problem. My view lays out images in the view constructor. The only time you know the actual view height is on the onMesure method by doing:

   protected void onMeasure (int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    widthView = MeasureSpec.getSize(widthMeasureSpec);
    heightView = MeasureSpec.getSize(heightMeasureSpec);
   }

However I can't lay out the images inside this method (animation of the images will keep calling this method).

To know the actual height of the view I would need to do:

screenHeight - statusBarHeight

so I could use this value on the constructor of the view.

I can't believe it has to be so complex to provide a solution for such a simple requirement.

sacchetto
A: 

Yeah - thanks Xamar - been searching the net for ages trying to get this. Why isn't it an obvious in the Android SDK? Really wierd....

Lemonsanver