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 the "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 the "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. public class MainActivity extends AppCompatActivity {
  13. private Context mContext;
  14. private TextView mTextViewInfo;
  15. private TextView mTextViewPercentage;
  16. private ProgressBar mProgressBar;
  17. private int mProgressStatus = 0;
  18. private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
  19. @Override
  20. public void onReceive(Context context, Intent intent) {
  21. int scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE,-1);
  22. mTextViewInfo.setText("Battery Scale : " + scale)
  23. int level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL,-1);
  24. mTextViewInfo.setText(mTextViewInfo.getText() + "\nBattery Level : " + level);
  25. float percentage = level/ (float) scale;
  26. mProgressStatus = (int)((percentage)*100);
  27. mTextViewPercentage.setText("" + mProgressStatus + "%");
  28. mTextViewInfo.setText(mTextViewInfo.getText() +
  29. "\nPercentage : "+ mProgressStatus + "%");
  30. mProgressBar.setProgress(mProgressStatus);
  31. }
  32. };
  33. @Override
  34. protected void onCreate(Bundle savedInstanceState) {
  35. requestWindowFeature(Window.FEATURE_ACTION_BAR);
  36. super.onCreate(savedInstanceState);
  37. setContentView(R.layout.activity_main);
  38. mContext = getApplicationContext();
  39. IntentFilter iFilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
  40. mContext.registerReceiver(mBroadcastReceiver,iFilter);
  41. mTextViewInfo = (TextView) findViewById(R.id.tv_info);
  42. mTextViewPercentage = (TextView) findViewById(R.id.tv_percentage);
  43. mProgressBar = (ProgressBar) findViewById(R.id.pb);
  44. }
  45. }
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 the menu bar and click "Make a project" or press ctrl+f9 to debug the error.
Battery Level Android App
Step 10
Then, click the 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 an app that shows the battery level of our Android phone.
Battery Level Android App