Recently, as the importance of mobile app development has increased, many developers are learning to develop apps on the Android platform. This course will explain in detail how to create a simple app that displays battery information using Kotlin. This course will be a great opportunity to understand the basic concepts of Kotlin and to identify the essential elements needed for Android app development.
1. Setting Up the Development Environment
To start Android app development, you first need to install the necessary tools. The commonly used IDE is Android Studio. Android Studio supports development with Kotlin and includes a powerful code editor and debugging tools.
- Download and install Android Studio: Download and install the latest version from the official Android Studio website.
- Install SDK and Emulator: The first time you run Android Studio, you will be prompted to install the SDK (SDK Manager) and Emulator. This process is mandatory.
- Check Kotlin Plugin: Android Studio supports Kotlin by default, but check and update the plugin if necessary.
2. Creating a New Project
Creating a new project in Android Studio is straightforward.
- Open Android Studio and select Start a new Android Studio project.
- Choose Empty Activity as the project template.
- Set the project name to BatteryInfoApp, and designate the package name as com.example.batteryinfo.
- Select Kotlin and set the minimum SDK to API 21 (Lollipop).
- Finally, click Finish to create the project.
3. Understanding the App Structure
Android apps generally consist of components such as Activity, Fragment, and Service. In this example, we will use MainActivity as the main activity to display battery information.
When the project is created, the app/src/main/java/com/example/batteryinfo/MainActivity.kt file and app/src/main/res/layout/activity_main.xml file will be generated.
4. Reading Battery Information
In Android, you can read battery information using system services and BroadcastReceiver. We will use BatteryManager and BroadcastReceiver to retrieve battery information.
4.1. Writing the Code
First, we will define the layout to display battery information. Modify the activity_main.xml file to add a TextView that shows battery status and percentage.
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:id="@+id/battery_percentage" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:textSize="24sp" android:text="Battery: 0%" android:padding="16dp"/> <TextView android:id="@+id/battery_status" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/battery_percentage" android:layout_centerHorizontal="true" android:textSize="18sp" android:text="Status: Unknown"/> </RelativeLayout>
Now, we will modify the MainActivity.kt file to add the logic for reading battery information. Write the code as shown below.
package com.example.batteryinfo import android.content.BroadcastReceiver import android.content.Context import android.content.Intent import android.content.IntentFilter import android.os.BatteryManager import androidx.appcompat.app.AppCompatActivity import android.os.Bundle import android.widget.TextView class MainActivity : AppCompatActivity() { private lateinit var batteryPercentage: TextView private lateinit var batteryStatus: TextView override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) batteryPercentage = findViewById(R.id.battery_percentage) batteryStatus = findViewById(R.id.battery_status) val batteryStatusIntent = registerReceiver(null, IntentFilter(Intent.ACTION_BATTERY_CHANGED)) val batteryLevel = batteryStatusIntent?.getIntExtra(BatteryManager.EXTRA_LEVEL, -1) ?: -1 val batteryScale = batteryStatusIntent?.getIntExtra(BatteryManager.EXTRA_SCALE, -1) ?: -1 val batteryPercent = (batteryLevel / batteryScale.toFloat() * 100).toInt() batteryPercentage.text = "Battery: $batteryPercent%" when (batteryStatusIntent?.getIntExtra(BatteryManager.EXTRA_STATUS, -1)) { BatteryManager.BATTERY_STATUS_CHARGING -> { batteryStatus.text = "Status: Charging" } BatteryManager.BATTERY_STATUS_DISCHARGING -> { batteryStatus.text = "Status: Discharging" } BatteryManager.BATTERY_STATUS_FULL -> { batteryStatus.text = "Status: Full" } else -> { batteryStatus.text = "Status: Unknown" } } } }
4.2. Explanation
In the above code, we are reading the battery status and percentage through BatteryManager. The battery status is checked via BatteryManager.EXTRA_STATUS, and the percentage is calculated through the battery level and scale. Subsequently, this information is displayed in the TextView.
5. Running and Testing the App
Now that the app is ready, let’s run it on a real device or emulator. You can run the app by clicking the Run button in the upper menu of Android Studio or pressing Shift + F10. When the app runs on the emulator, the battery status and percentage will be displayed on the screen.
6. Implementing Additional Features
After creating the basic battery information app, let’s consider a few additional features that can be implemented.
6.1. Detecting Battery Changes
It is also possible to detect changes in battery status and update the UI accordingly. To do this, you will need to use a BroadcastReceiver to receive battery status change events and update the UI. The following code can be added.
private val batteryReceiver = object : BroadcastReceiver() { override fun onReceive(context: Context?, intent: Intent?) { val level = intent?.getIntExtra(BatteryManager.EXTRA_LEVEL, -1) ?: -1 val scale = intent?.getIntExtra(BatteryManager.EXTRA_SCALE, -1) ?: -1 val batteryPercent = (level / scale.toFloat() * 100).toInt() batteryPercentage.text = "Battery: $batteryPercent%" when (intent?.getIntExtra(BatteryManager.EXTRA_STATUS, -1)) { BatteryManager.BATTERY_STATUS_CHARGING -> { batteryStatus.text = "Status: Charging" } BatteryManager.BATTERY_STATUS_DISCHARGING -> { batteryStatus.text = "Status: Discharging" } BatteryManager.BATTERY_STATUS_FULL -> { batteryStatus.text = "Status: Full" } else -> { batteryStatus.text = "Status: Unknown" } } } } override fun onStart() { super.onStart() val filter = IntentFilter(Intent.ACTION_BATTERY_CHANGED) registerReceiver(batteryReceiver, filter) } override fun onStop() { super.onStop() unregisterReceiver(batteryReceiver) }
6.2. Improving UI Design
Based on the basic layout, you can add battery icons or graphs to make the UI more intuitive. Adding various design elements with user experience (UX) in mind is also a good direction.
7. Conclusion
In this course, we created a simple battery information app using Kotlin. We learned the basic method of reading battery information and will be able to grow as an app developer by adding various features in the future. We encourage continuous interest and learning in Android app development. Try to create your own great apps through various tools and frameworks!