Android Battery View

Android Battery View (ABV)

Introduction

This library was born out of necessity during the development of the Device Info app. I encountered the challenge of presenting battery level and status in a graphical format, yet struggled to find a suitable option that was easy to implement. Despite discovering a few libraries on GitHub, none fulfilled all my requirements, and to my dismay, many were no longer maintained. Faced with this dilemma, I took matters into my own hands and developed Android Battery View (ABV).

ABV was created hastily, with plenty of room for improvement. However, it currently serves its purpose as is.

Features

This one has pretty straightfoward features. 

Charge Level

  • Accurately displays the charge level from 1 to 100, with each level depicted on the canvas.

Stats

Includes four distinct stats:

  • Normal: Indicates discharging status.
  • Charging: Represents when the device is charging.
  • Warning: Indicates a low battery level, albeit not critically low.
  • Critical: Signals a critically low battery, prompting immediate action to find a charger.

Customization

Offers customization options:

  • Ability to change the color for all four stats.
  • Icons associated with charging, warning, and critical stats can be replaced with custom ones.
  • Compatible with both portrait and landscape modes.

BroadcastReceiver

Enables attachment to a BroadcastReceiver intent:

  • Automatically retrieves battery data from the intent, ensuring seamless integration.

Demo

NormalChargingWarningCritical
Shot1Shot2Shot3Shot4

Technologies Used

This library is entirely coded in Kotlin, with additional assistance from XML for attribute creation. Android Studio serves as the primary Integrated Development Environment (IDE) for development.

The source code is proudly hosted on GitHub.

Workflow

  1. Planning: I began by examining existing repositories to gather ideas on how I wanted the library to function. I meticulously noted the limitations of these projects and devised a plan based on these observations.

  2. Draft UI: Utilizing Figma, I created initial drafts and collected the necessary icons required for the project.

  3. Core Coding: The development process kicked off with the core coding phase in Kotlin. Starting from the basic structure, I gradually built upon it, shaping it into its final form.

  4. Finalizing: With the core functionality in place, I dedicated time to refining the library, incorporating additional tweaks and enhancements to ensure its optimal performance and usability.

Problem and Solution

Canvas Resources

Problem: There is a scarcity of resources available on canvas manipulation. While some outdated tutorials exist, they often lack depth and fail to address advanced topics. Consequently, encountering obstacles during development proved challenging, with few avenues for finding solutions beyond basic documentation.

Solution: In the face of this challenge, I adopted a hands-on approach. Testing various theories and techniques, I delved into the intricacies of canvas manipulation. Through perseverance and experimentation, I uncovered solutions to the obstacles encountered along the way. Embracing the age-old tactic of brute-force problem-solving, I navigated through the complexities, ultimately overcoming the limitations imposed by the lack of comprehensive resources.

How to Use

First you need to add the dependency in your build.gradle  or build.gradle.kts and then just add the XML and Kotlin code. 

Dependency

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

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

Broadcast Receiver

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)
    }
}

You can find the whole documentation here - https://github.com/ahmmedrejowan/AndroidBatteryView

What I've Learned

  • Developing a solution from scratch requires confronting unfamiliar territories and embracing challenges with an open mind. This experience has made me more receptive to encountering and overcoming new obstacles in future projects.
  • The project provided an opportunity to delve deeper into the Android Canvas, enhancing my understanding and proficiency in this area. Experimentation and hands-on exploration have facilitated a more comprehensive grasp of canvas manipulation techniques and capabilities.
  • Necessity often drives innovation. By venturing into uncharted territory, I've honed my ability to think creatively and devise novel solutions to complex problems. This mindset of innovation will undoubtedly prove beneficial in tackling future projects.

Source Code 

The source code and the whole project is available on GitHub. 

Repository Link - https://github.com/ahmmedrejowan/AndroidBatteryView

Repo Page - https://rejowan.com/AndroidBatteryView

Conclusion

In conclusion, the process of developing this new solution has been a journey of challenges and learning. Through experimentation and perseverance, I've expanded my knowledge of Android Canvas and cultivated a mindset of innovation. While there were obstacles along the way, overcoming them has equipped me with valuable skills and experiences for future projects.