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.
Normal | Charging | Warning | Critical |
---|---|---|---|
![]() |
![]() |
![]() |
![]() |
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' }
}
}
Add this to your module’s build.gradle.kts
file (latest version ):
dependencies {
...
implementation("com.github.ahmmedrejowan:AndroidBatteryView:0.1")
}
dependencies {
...
implementation 'com.github.ahmmedrejowan:AndroidBatteryView:0.1'
}
<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"/>
val abv = binding.abv
abv.size = 50
abv.mRadius = 10f
abv.chargeLevel = 50
abv.batteyOrientation = BatteryOrientation.PORTRAIT
abv.isCharging = false
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)
}
}
<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" />
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
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 |
Issues
tab.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.
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.