4

I'm currently working on an app to display the battery status and I'd like to use Android-drawables instead of own images to reduce the app size.

I've found this page which lists available images and the availability for each SDK-version:
http://www.fixedd.com/projects/android_drawables_display

My question: How can I access the "system"-drawables? If you click on the link and choose the tab "Status", there are some battery-drawables like "stat_sys_battery_0", but I can't access it, Eclipse doesn't offer intellisense for it and won't compile the app if I use one of those drawables.

As those drawables are part of all SDK-versions, I'd think I should be able to use them, or are those "special" drawables protected in a way so they can only be used by system-functions (and not apps)?

Any idea is appreciated.

Select0r

skaffman
  • 398,947
  • 96
  • 818
  • 769
Select0r
  • 12,234
  • 11
  • 45
  • 68

3 Answers3

5

Hope this is what you were looking for:

private BroadcastReceiver mBatInfoReceiver = new BroadcastReceiver(){
    @Override
    public void onReceive(Context arg0, Intent intent) {

        int level = intent.getIntExtra("level", 0);

        int batteryIconId = intent.getIntExtra("icon-small", 0);
        Button toolBarBattery = (Button) findViewById(R.id.toolBarButton);

        LevelListDrawable batteryLevel = (LevelListDrawable) getResources().getDrawable(batteryIconId);
        batteryLevel.setLevel(level);

        toolBarBattery.setBackgroundDrawable(batteryLevel);

    }
};
rttn
  • 51
  • 1
  • This is actually almost the same as my (accepted) solution from two weeks earlier. The only difference is that you're using "icon-small" and I'm using `BatteryManager.EXTRA_ICON_SMALL`. – Select0r Mar 05 '11 at 21:12
  • 1
    "However, it always returns the same icon, no matter what the actual battery level is..." the use of the LevelListDrawable object solves that issue, try it out if you want ;) – rttn Mar 11 '11 at 15:02
2

I've found another link with information that not all drawables are public. It doesn't say why some drawables would be private, but I guess I'll have to live with the fact and copy the needed images to my app.
http://androiddrawableexplorer.appspot.com/

NOTE: Some of the images in the Android jar are not public and therefore cannot be directly used (you can copy them to you own application, but can't reference them via the "android" package namespace).

Select0r
  • 12,234
  • 11
  • 45
  • 68
  • +1 glad you found an answer to this. I ran into this problem before and put it off to the fact I was keeping a 1.6 compatible build environment. – Will Tate Feb 03 '11 at 21:18
1

There actually seems to be a way to access the system icons, but it's not really working as stated in the documentation, but I'll add it in case somebody is interested:

intent.getIntExtra(BatteryManager.EXTRA_ICON_SMALL, -1)

Will get you the resource-ID of the icon that matches the current battery-status:
http://developer.android.com/reference/android/os/BatteryManager.html#EXTRA_ICON_SMALL

Extra for ACTION_BATTERY_CHANGED: integer containing the resource ID of a small status bar icon indicating the current battery state.

However, it always returns the same icon, no matter what the actual battery level is. Finding the icon by just trying random numbers may work, but I don't know if the IDs are consistent throughout the SKD-levels as well as different machines, so I'd rather not rely in that.

Select0r
  • 12,234
  • 11
  • 45
  • 68