0

I want to show the battery level of phone using images and these images will change dynamically as battery level increases or decreases. Is their any specific API for this ? Any reference for this would be great..

Amit
  • 12,400
  • 16
  • 71
  • 142
  • You can just type "imageHeightPercent = batteryPercent"! Oh, wait, no that's not it. Maybe start with working out getting the battery level. This seems useful: http://stackoverflow.com/questions/3291655/get-battery-level-and-state-in-android – Bitwise Creative Feb 05 '15 at 05:19
  • I acquired the level. But how can I make it show images – user4531452 Feb 12 '15 at 07:50

2 Answers2

2

Make a Receiver in your class where you want to show battery level like

private BroadcastReceiver mBatInfoReceiver = new BroadcastReceiver(){
    @Override
    public void onReceive(Context ctxt, Intent intent) {
      int level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, 0);
      batteryTxt.setText(String.valueOf(level) + "%");

      if(level > 90)
        img_battery.setImageResource(R.drawable.battery9);
      else if(level > 80)
        img_battery.setImageResource(R.drawable.battery8);
      else if(level > 70)
        img_battery.setImageResource(R.drawable.battery7);
      else if(level > 60)
        img_battery.setImageResource(R.drawable.battery6);
      else if(level > 50)
        img_battery.setImageResource(R.drawable.battery5);
      else if(level > 35)
        img_battery.setImageResource(R.drawable.battery4);
      else if(level > 20)
        img_battery.setImageResource(R.drawable.battery3);
      else if(level > 5)
        img_battery.setImageResource(R.drawable.battery2);
      else if(level > 2)
        img_battery.setImageResource(R.drawable.battery1);
      else 
        img_battery.setImageResource(R.drawable.battery0);

    }
};

and register the reciever in onCreate

this.registerReceiver(this.mBatInfoReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));

don't forget to un-register it as well in onDestroy

@Override
protected void onDestroy() {
    this.unregisterReceiver(this.mBatInfoReceiver);
    super.onDestroy();
}

and img_battery would be the ImageView in which you are showing battery level, and batteryTxt would be the TextView which shows exact percentage.

Nanites
  • 389
  • 2
  • 16
  • if I add this sir, would this display battery level via image? And If I will use the smali file of this, i will use – user4531452 Feb 05 '15 at 06:09
  • For making a separate file for this you will have to pass the context of the activity and make the respective calls in onDestroy and onCreate. R.drawable.battery9 .... R.drawable.battery0 are the images of battery representing the different battery levels. – Nanites Feb 05 '15 at 06:16
  • im new to this things sir. How will I be able to start the java file? I choosed programming to be my hobby. I dont know so much things – user4531452 Feb 05 '15 at 06:21
  • 1
    do you have a facebook account sir? So we can talk more clearly? – user4531452 Feb 05 '15 at 06:30
  • Are there multiple activities in which you want to show battery level indicator? facebook id 100007971587388 – Nanites Feb 05 '15 at 06:33
  • Please please please unregister in onPause and re-register in onResume! Otherwise the Activity will keep waking up and doing stuff for no reason, even when it's not visible. – Don Hatch Apr 20 '18 at 03:29
0

You can choose to custom a view for your battery life.A wrapper rect with a inner indicator rect. With different level of battery, you can draw the inner container with different width and color. Draw png is also approachable

tinaJohnny
  • 123
  • 1
  • 9