Exercise 2 Creating a battery level using Android Studio - srihawi/AndroidExercises GitHub Wiki

In this exercise we will create an Android app to show the battery level on the device

Let's get started with our Battery Level android App: Step-1 Open your Android Studio Click on Start a New Android Studio Project. Give your Application Name BatteryLevelProject and leave other fields blank as it is, then click NEXT.

Step-2 Select the Empty Activity and click NEXT. Select the Minimum SDK below based on the target devices Leave the activity name MainActivity as it is and leave everything as it is. Click Finish.

Step-3 After clicking Finish, it takes around around ~2 minutes to build Activity and files. Here is a final project structure for your application.

Step-4 Now we have to add our Java code in our MainActivity.java file.

 package com.example.batterylevelproject;

 import android.content.BroadcastReceiver;
 import android.content.Context;
 import android.content.Intent;
 import android.content.IntentFilter;
 import android.content.res.AssetFileDescriptor;
 import android.media.MediaPlayer;
 import android.os.BatteryManager;
 import android.os.Bundle;
 import android.widget.ProgressBar;
 import android.widget.TextView;

 import androidx.appcompat.app.AppCompatActivity;

 import java.io.IOException;

 public class MainActivity extends AppCompatActivity {

 TextView mBatteryLevelText;
 ProgressBar mBatteryLevelProgress;
 BroadcastReceiver mReceiver;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mBatteryLevelText = (TextView) findViewById(R.id.textView);
    mBatteryLevelProgress = (ProgressBar) findViewById(R.id.progressBar);

    mReceiver = new BatteryBroadcastReceiver();
}

@Override
protected void onStart() {
    registerReceiver(mReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
    super.onStart();
}

@Override
protected void onStop() {
    unregisterReceiver(mReceiver);
    super.onStop();
}


class BatteryBroadcastReceiver extends BroadcastReceiver {
    private final static String BATTERY_LEVEL = "level";

    @Override
    public void onReceive(Context context, Intent intent) {
        int level = intent.getIntExtra(BATTERY_LEVEL, 0);

        mBatteryLevelText.setText("Battery Level " + level);
        mBatteryLevelProgress.setProgress(level);
    }
}

}

Below is the layout file, with help to design front end for the app:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"

    android:orientation="vertical"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/textView"
        android:text="Battery Level"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <ProgressBar
        android:id="@+id/progressBar"
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_marginTop="8dp"
        android:layout_marginLeft="12dp"
        android:layout_marginRight="12dp"
        android:max="100"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView"
        android:layout_toRightOf="@+id/textView"
        android:layout_toEndOf="@+id/textView" />
</LinearLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

Now, all things should works fine and we are ready to run our calculator android app. You can run the app using your mobile device or an emulator.

Running our Battery Level Android App Click on Android device manager. After selecting your custom device in Android device manager window, click START. Click on Run button. Choose Your device or emulator and click OK. Now you can see calculator android app running. Congratulations!! If you have followed all steps and reach this point means you have followed all the steps correctly and your Battery Level android app is up and running fine.