1

I want to examine whether the battery icon and level in status bar are changed correctly, so I want to get the Battery Icon Resource ID or the full file path/name in Status Bar.

I tried to use the Battery Manager to get the icon's resource ID:

intent.getIntExtra(BatteryManager.EXTRA_ICON_SMALL, 0)

However, I encountered the same problem as the author did: Android: access "system"-drawables

I get the same icon, whose path is /res/drawable/stat_sys_battery.xml, no matter what the level is. I am eager to know why is this icon not the same with status bar's icon? My goal is to get the status bar's icon.

I have searched for the answer for quite a while, but most posts on this topic pertain to changing the battery icon in status bar, whereas my question is on how to get the status bar's status and icon, i.e. I do not want to modify the icon.

Please help me! Thanks in advance.

Community
  • 1
  • 1
Ywis
  • 180
  • 1
  • 4
  • 13

1 Answers1

3

I am eager to know why this icon is not the same with status bar's icon.

It is the same as the status bar's icon, because it is the status bar's icon. The battery level is a LevelListDrawable as implemented in that resource.

Unless you are building your own custom firmware, you do not have access to the actual LevelListDrawable object, and therefore you cannot examine it.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Thank you very much! I did not notice this icon is a LevelListDrawable! The reason why I got a different icon from status bar is because what I got is a default icon. See http://developer.android.com/reference/android/graphics/drawable/LevelListDrawable.html – Ywis Mar 12 '12 at 01:39
  • It still looks different for me-- status bar's icon is dark-gray-lightningbolt-on-white-and-dark-gray-battery, the icon I get is white-lightningbolt-on-gray-and-dark-gray battery. Any idea why? – Don Hatch Apr 20 '18 at 04:39
  • @DonHatch: On Android 5.0+, status bar icons are supposed to be pure alpha channel, so that they can be adapted to be either black or white based on the color of the status bar (since apps affect the status bar color). I cannot explain why you are seeing any other color. – CommonsWare Apr 20 '18 at 09:29
  • @CommonsWare Do you have a reference for the bit about being pure alpha and adapting to different color backgrounds? I observe that the battery icon in both my app and in my status bar (Oreo) have a bit of red at the bottom when the battery is suffficiently low-- do you see that too? So it has red as well as shades of gray; that can't possibly be simply a pure alpha image, can it? – Don Hatch Apr 20 '18 at 10:27
  • @DonHatch: See https://developer.android.com/guide/practices/ui_guidelines/icon_design_status_bar.html – CommonsWare Apr 20 '18 at 10:31
  • Thanks. So apparently the system battery icon doesn't conform to those guidelines? Looking through AOSP source, I believe one of the icons I'm seeing in my app and/or status bar is frameworks/base/core/res/res/drawable-mdpi/stat_sys_battery_15.png (see the red in it). Strange, I can't find that dark-lightningbolt-on-lighter-background that I saw in my status bar on Oreo, in any battery .png files in AOSP source. – Don Hatch Apr 20 '18 at 11:22
  • @DonHatch: I have no idea what devices necessarily use AOSP-supplied drawables. I would have had reasonable faith that Nexus devices did, but I would not be surprised if Pixel devices do not. And any manufacturer is welcome to do whatever they want. In terms of AOSP not following Google's own guidelines, that does not shock me in the slightest. – CommonsWare Apr 20 '18 at 20:19