AndroidBatteryView

Andrid Battery View

platform API JitPack GitHub license

A lightweight Battery Status View

GitHub issues GitHub forks GitHub stars GitHub contributors

Table of Contents

Purpose

The Android Battery View (ABV) library was born out of frustration with the time-consuming task of creating a battery view component for Android apps. Developed to avoid repetitive tasks and streamline development, ABV offers a simple solution for integrating a customizable battery view. It aims to save developers time and effort, enhancing user experience by providing a visually appealing representation of battery status without the hassle of reinventing the wheel.

Features

Demo

Normal Charging Warning Critical
Shot1 Shot2 Shot3 Shot4

Prerequisites

Kotlin DSL

dependencyResolutionManagement {
    repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
    repositories {
        google()
        mavenCentral()
        maven{
            url = uri("https://jitpack.io")
        }
    }
}
dependencyResolutionManagement {
		repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
		repositories {
			mavenCentral()
			maven { url 'https://jitpack.io' }
		}
	}

Dependency

Add this to your module’s build.gradle.kts file (latest version JitPack):

dependencies {
    ...
    implementation("com.github.ahmmedrejowan:AndroidBatteryView:0.1")
}
dependencies {
    ...
    implementation 'com.github.ahmmedrejowan:AndroidBatteryView:0.1'
}

Usage

XML

<com.rejowan.abv.ABV
    android:id="@+id/abv"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:abvBatteryOrientation="portrait"
    app:abvRadius="10"
    app:abvSize="50"/>

Kotlin

val abv = binding.abv
abv.size = 50
abv.mRadius = 10f
abv.chargeLevel = 50
abv.batteyOrientation = BatteryOrientation.PORTRAIT
abv.isCharging = false

BroadcastReceiver

This is the most unique feature of the library. You can attach your broadcast receiver intent to this view and it’ll automatically show the status of the device battery.

Benefits

Here is an example

class MainActivity : AppCompatActivity() {

    private val binding: ActivityMainBinding by lazy {ActivityMainBinding.inflate(layoutInflater)}

    private val batteryReceiver: BroadcastReceiver = object : BroadcastReceiver() {
        override fun onReceive(context: Context?, intent: Intent?) {
            if (intent!=null){
                binding.abv.attachBatteryIntent(intent)
            }
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(binding.root)
        registerReceiver(batteryReceiver, IntentFilter(Intent.ACTION_BATTERY_CHANGED))

    }

    override fun onDestroy() {
        super.onDestroy()
        unregisterReceiver(batteryReceiver)
    }
}

Customization

XML

<com.rejowan.abv.ABV
    android:id="@+id/abv"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    app:abvNormalBgColor="#86B6F6"
    app:abvNormalLevelColor="#4E94F1"
    app:abvWarningBgColor="#FFCF96"
    app:abvWarningLevelColor="#F5AD56"
    app:abvCriticalBgColor="#EF5350"
    app:abvCriticalLevelColor="#B71C1C"
    app:abvChargingBgColor="#89EC9E"
    app:abvChargingLevelColor="#4DD86C"
    app:abvChargeLevel="5"
    app:abvWarningChargeLevel="30"
    app:abvCriticalChargeLevel="10"
    app:abvIsCharging="false"
    app:abvRadius="10"
    app:abvBatteryOrientation="portrait"
    app:abvSize="50"
    app:abvChargingIcon="@drawable/ic_charge_abv"
    app:abvWarningIcon="@drawable/ic_warning_abv" />

Kotlin

val abv = binding.abv
abv.normalBackgroundColor = ContextCompat.getColor(this, R.color.yourColor)
abv.normalLevelColor = ContextCompat.getColor(this, R.color.yourColor)
abv.warningBackgroundColor = ContextCompat.getColor(this, R.color.yourColor)
abv.warningLevelColor = ContextCompat.getColor(this, R.color.yourColor)
abv.criticalBackgroundColor = ContextCompat.getColor(this, R.color.yourColor)
abv.criticalLevelColor = ContextCompat.getColor(this, R.color.yourColor)
abv.chargingBackgroundColor = ContextCompat.getColor(this, R.color.yourColor)
abv.chargingLevelColor = ContextCompat.getColor(this, R.color.yourColor)
abv.chargingIcon = R.drawable.ic_charge_abv
abv.warningIcon = R.drawable.ic_warning_abv
abv.size = 50
abv.mRadius = 10f
abv.chargeLevel = 50
abv.warningChargeLevel = 30
abv.criticalChargeLevel = 10
abv.batteyOrientation = BatteryOrientation.PORTRAIT
abv.isCharging = false

Attribute

Full list of attributes available

Attribute Format Description
abvNormalBgColor color Background color for normal state
abvNormalLevelColor color Level color for normal state
abvWarningBgColor color Background color for warning state
abvWarningLevelColor color Level color for warning state
abvCriticalBgColor color Background color for critical state
abvCriticalLevelColor color Level color for critical state
abvChargingBgColor color Background color for charging state
abvChargingLevelColor color Level color for charging state
abvChargeLevel integer Level of charge
abvWarningChargeLevel integer Warning level of charge
abvCriticalChargeLevel integer Critical level of charge
abvIsCharging boolean Indicates whether the device is charging
abvRadius integer Radius of the battery view
abvBatteryOrientation enum Orientation of the battery view
abvSize integer Size of the battery view
abvChargingIcon reference Icon displayed when charging
abvWarningIcon reference Icon displayed in warning state

Notes

Contribute

Please fork this repository and contribute back using pull requests.

Any contributions, large or small, major features, bug fixes, are welcomed and appreciated.

Let me know which features you want in the future in Request Feature tab.

If this project helps you a little bit, then give a to Star ⭐ the Repo.

License

Copyright 2024 ahmmedrejowan

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

 http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.