Battery Level Android App Using Android Studio

Introduction

In this article, I will show you how to create an Android app that shows the Battery Level of your device. A battery is an electrochemical cell (or enclosed and protected material) that can be charged electrically to provide a static potential for power or released electrical charge when needed.

 Requirements

Steps to be followed

Follow these steps to create the app using Android Studio. I have attached the source code too.

Step 1

Open Android Studio and start a new Android Studio Project.

Battery Level Android App

Step 2

You can choose your application name and choose where your project is stored. If you wish to use C++ for coding the project, mark the "Include C++ support", and click "Next" button.

Battery Level Android App

Step 3

Now, select the version of Android and select the target Android devices. We need to choose the SDK level which plays an important role to run the application.

Battery Level Android App

Step 4

Now, add the activity and click "Next" button.

Battery Level Android App

Step 5

Add Activity name and click "Finish".

Battery Level Android App

Step 6

Go to activity_main.xml. This XML file contains the designing code for the Android app.

Battery Level Android App

The XML code is given below.

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout  
  3.     xmlns:android="http://schemas.android.com/apk/res/android"  
  4.     xmlns:tools="http://schemas.android.com/tools"  
  5.     android:layout_width="match_parent"  
  6.     android:layout_height="match_parent"  
  7.     android:padding="16dp"  
  8.     tools:context=".MainActivity"  
  9.     android:background="#d5ebfa"  
  10.     >  
  11.     <ProgressBar  
  12.         android:id="@+id/pb"  
  13.         android:layout_width="150dp"  
  14.         android:layout_height="150dp"  
  15.         style="@android:style/Widget.ProgressBar.Horizontal"  
  16.      android:progressDrawable="@layout/progressbar_states"  
  17.         android:layout_centerInParent="true"  
  18.         />  
  19.     <TextView  
  20.         android:id="@+id/tv_percentage"  
  21.         android:layout_width="wrap_content"  
  22.         android:layout_height="wrap_content"  
  23.         android:textSize="25dp"  
  24.         android:textColor="#000"  
  25.         android:layout_centerInParent="true"  
  26.         />  
  27.     <TextView  
  28.         android:id="@+id/tv_info"  
  29.         android:layout_width="wrap_content"  
  30.         android:layout_height="wrap_content"  
  31.         android:textSize="20dp"  
  32.         />  
  33. </RelativeLayout>  

Step 7

Go to Main Activity.java. This Java program is the back-end language of Android.

Battery Level Android App

The java code is given below.

  1. package abu.battery;  
  2. import android.content.BroadcastReceiver;  
  3. import android.content.Context;  
  4. import android.content.Intent;  
  5. import android.content.IntentFilter;  
  6. import android.os.BatteryManager;  
  7. import android.support.v7.app.AppCompatActivity;  
  8. import android.os.Bundle;  
  9. import android.view.Window;  
  10. import android.widget.ProgressBar;  
  11. import android.widget.TextView;  
  12.   
  13. public class MainActivity extends AppCompatActivity {  
  14.     private Context mContext;  
  15.   
  16.     private TextView mTextViewInfo;  
  17.     private TextView mTextViewPercentage;  
  18.     private ProgressBar mProgressBar;  
  19.     private int mProgressStatus = 0;  
  20.     private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {  
  21.         @Override  
  22.         public void onReceive(Context context, Intent intent) {  
  23.   
  24.             int scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE,-1);  
  25.   
  26.             mTextViewInfo.setText("Battery Scale : " + scale)  
  27.   
  28.             int level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL,-1);  
  29.             mTextViewInfo.setText(mTextViewInfo.getText() + "\nBattery Level : " + level);  
  30.             float percentage = level/ (float) scale;  
  31.             mProgressStatus = (int)((percentage)*100);  
  32.             mTextViewPercentage.setText("" + mProgressStatus + "%");  
  33.             mTextViewInfo.setText(mTextViewInfo.getText() +  
  34.                     "\nPercentage : "+ mProgressStatus + "%");  
  35.             mProgressBar.setProgress(mProgressStatus);  
  36.         }  
  37.     };  
  38.     @Override  
  39.     protected void onCreate(Bundle savedInstanceState) {  
  40.         requestWindowFeature(Window.FEATURE_ACTION_BAR);  
  41.         super.onCreate(savedInstanceState);  
  42.         setContentView(R.layout.activity_main);  
  43.         mContext = getApplicationContext();  
  44.         IntentFilter iFilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);  
  45.         mContext.registerReceiver(mBroadcastReceiver,iFilter);  
  46.         mTextViewInfo = (TextView) findViewById(R.id.tv_info);  
  47.         mTextViewPercentage = (TextView) findViewById(R.id.tv_percentage);  
  48.         mProgressBar = (ProgressBar) findViewById(R.id.pb);  
  49.     }  
  50. }  

Step 8

Go to (App ⇒ Res ⇒Layout⇒progressbar_states.xml).

Battery Level Android App

The Progressbar_states.xml code is given below.

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <layer-list xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_height="match_parent" android:layout_width="match_parent">  
  4.     <item android:id="@android:id/background">  
  5.         <shape  
  6.             android:shape="oval">  
  7.             <stroke  
  8.                 android:width="5dp"  
  9.                 android:color="#d0d4d7"  
  10.                 />  
  11.             <solid android:color="#eef2f5"/>  
  12.         </shape>  
  13.     </item>  
  14.     <item android:id="@android:id/progress">  
  15.         <clip android:clipOrientation="vertical" android:gravity="bottom">  
  16.             <shape  
  17.                 android:shape="oval">  
  18.                 <stroke  
  19.                     android:width="5dp"  
  20.                     android:color="#ff0001"  
  21.                     />  
  22.                 <gradient  
  23.                     android:startColor="#7fc97c"  
  24.                     android:endColor="#99f396"  
  25.                     android:centerColor="#89e386"  
  26.                     android:angle="270"  
  27.                     android:gradientRadius="50"  
  28.                     android:centerY=".50"  
  29.                     />  
  30.             </shape>  
  31.         </clip>  
  32.     </item>  
  33. </layer-list>  

Step 9

Now, go to menu bar and click "Make project" or press ctrl+f9 to debug the error.

Battery Level Android App

Step 10

Then, click Run button or press shift+f10 to run the project. And, choose the virtual machine option and click OK.

Battery Level Android App

Conclusion

We have successfully created the app that shows the battery level of our Android phone.

Battery Level Android App

Up Next
    Ebook Download
    View all
    Learn
    View all