X

Live Battery Status With Animation in Android Programming

TechJunkGigs

Live Battery Status With Animation in Android Programming, In this tutorial we are going to discuss how create animation in android application with live status of mobile battery.

Java File

In this section, name of the java file is MainActivity.java paste the below code in this file. Change Package name as per your package.

package com.example.jamaley.batterystatus;

import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Color;
import android.os.BatteryManager;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;
import me.itangqi.waveloadingview.WaveLoadingView;
/**
 * Created by jamaley on 7/17/2017.
 */

public class MainActivity extends AppCompatActivity {
    int i;
    boolean isCharging, usbCharge, acCharge;
    WaveLoadingView mWaveLoadingView;
    TextView txtbattery_status, txtstatus, txtcharge_status, txtport;
    private int progressStatus = 0;
    private Handler handler = new Handler();


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mWaveLoadingView = (WaveLoadingView) findViewById(R.id.waveLoadingView);
        mWaveLoadingView.setShapeType(WaveLoadingView.ShapeType.CIRCLE);
        txtbattery_status= (TextView) findViewById(R.id.txtbattery_status);
        txtstatus= (TextView) findViewById(R.id.txtstatus);
        txtcharge_status= (TextView) findViewById(R.id.txtcharge_status);
        txtport= (TextView) findViewById(R.id.txtport);
        i=getBatteryLevel(getApplicationContext());
        //battery level conditions
        if(i>=50 && i<=75){
            txtstatus.setText("Battery Status: Moderate");
        }
        else if (i>=75 && i<=99){
            txtstatus.setText("Battery Status: GOOD");
        }
        else if (i>=20 && i<=49){
            txtstatus.setText("Battery Status: Draining");
        }
        else if (i>=10 && i<=20){
            txtstatus.setText("Battery Status: LOW");
        }
        else if (i<10){
            txtstatus.setText("Battery Status: Critically Low, Please Charge the Device!");
        }
        else if (i==100){

            txtstatus.setText("Battery Status: Fully Charged");
        }
        mWaveLoadingView.setTopTitle(i+"%");
        mWaveLoadingView.setCenterTitleColor(Color.WHITE);
        mWaveLoadingView.setCenterTitle("");
        mWaveLoadingView.setBottomTitleSize(18);
        mWaveLoadingView.setProgressValue(0);
        mWaveLoadingView.setBorderWidth(5);
        mWaveLoadingView.setAmplitudeRatio(100);
        mWaveLoadingView.setWaveColor(Color.parseColor("#B2DFDB"));
        mWaveLoadingView.setBorderColor(Color.parseColor("#4CAF50"));
        mWaveLoadingView.setTopTitleStrokeColor(Color.WHITE);
        mWaveLoadingView.setTopTitleStrokeWidth(3);
        mWaveLoadingView.setWaterLevelRatio(0.2f);
        mWaveLoadingView.setAnimDuration(3000);
        mWaveLoadingView.pauseAnimation();
        mWaveLoadingView.resumeAnimation();
        mWaveLoadingView.cancelAnimation();
        mWaveLoadingView.startAnimation();
        new Thread(new Runnable() {
            public void run() {
                while (progressStatus < i) {
                    progressStatus += 2;
                    // Update the progress bar and display the
                    //current value in the text view
                    handler.post(new Runnable() {
                        public void run() {
                            mWaveLoadingView.setProgressValue(progressStatus);
                            String x=String.valueOf(i);
                            txtbattery_status.setText("Battery Level:"+x+"%");
                            getChargingLevel(getApplicationContext());

                            //Battery charging conditions
                            if (isCharging==true && usbCharge==true){
                                txtcharge_status.setText("Charging Status: Device Charging");
                                txtport.setText("Connected to a USB Port");
                            }
                            else if(isCharging==true && acCharge==true){
                                txtcharge_status.setText("Charging Status: Device Charging");
                                txtport.setText("Connected to an AC power source");
                            }
                            else
                            {
                                txtcharge_status.setText("Device Not Charging");
                                txtport.setText( "Not Connected To Any Other Device");
                            }
                            if (isCharging==true && i==100){
                                txtcharge_status.setText("Charging Status: Charging Complete");
                            }


                        }
                    });
                    try {
                        // Sleep for 200 milliseconds.
                        //Just to display the progress slowly
                        Thread.sleep(200);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }).start();

        String x=String.valueOf(i);
        txtbattery_status.setText("Battery Level:"+x+"%");
        getChargingLevel(getApplicationContext());

        //Battery charging conditions
        if (isCharging==true && usbCharge==true){
            txtcharge_status.setText("Charging Status: Device Charging");
            txtport.setText("Connected to a USB Port");
        }
        else if(isCharging==true && acCharge==true){
            txtcharge_status.setText("Charging Status: Device Charging");
            txtport.setText("Connected to an AC power source");
        }
        else
        {
            txtcharge_status.setText("Device Not Charging");
            txtport.setText( "Not Connected To Any Other Device");
        }
        if (isCharging==true && i==100){
            txtcharge_status.setText("Charging Status: Charging Complete");
        }



    }
    //Checking the battery level.

    public int getBatteryLevel(Context context) {
        IntentFilter ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
        Intent batteryStatus = context.registerReceiver(null, ifilter);
        return i= batteryStatus.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);

    }

    //Checking the charging status.
    public void getChargingLevel(Context bl){
        IntentFilter ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
        Intent batteryStatus = bl.registerReceiver(null, ifilter);
        // Are we charging / charged?
        int status = batteryStatus.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
        isCharging = status == BatteryManager.BATTERY_STATUS_CHARGING ||
                status == BatteryManager.BATTERY_STATUS_FULL;

// How are we charging?
        int chargePlug = batteryStatus.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);
        usbCharge = chargePlug == BatteryManager.BATTERY_PLUGGED_USB;
        acCharge = chargePlug == BatteryManager.BATTERY_PLUGGED_AC;
    }



}

 

XML File

activity_main.xml
Now come to activity_main.xml file and write the below code.

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical">

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:gravity="center"
        android:layout_height="match_parent">
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="BATTERY MANAGER"
            android:textSize="25dp"
            android:gravity="center"
            android:textColor="#000000"
            />
        <me.itangqi.waveloadingview.WaveLoadingView
            android:id="@+id/waveLoadingView"
            android:layout_width="200dp"
            android:layout_height="200dp"
            app:wlv_borderColor="@color/colorAccent"
            app:wlv_borderWidth="3dp"
            app:wlv_progressValue="40"
            app:wlv_shapeType="circle"
            app:wlv_round_rectangle="true"
            app:wlv_triangle_direction="north"
            app:wlv_titleCenterStrokeWidth="3dp"
            app:wlv_waveColor="@color/colorAccent"/>
        <TextView
            android:layout_width="match_parent"
            android:padding="@dimen/abc_action_bar_icon_vertical_padding_material"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:textSize="25dp"
            android:text="Battery Status"
            android:id="@+id/txtbattery_status"
            />
        <TextView
            android:layout_width="match_parent"
            android:padding="@dimen/abc_action_bar_icon_vertical_padding_material"
            android:layout_height="50dp"
            android:layout_gravity="center"
            android:id="@+id/txtstatus"
            android:outlineProvider="paddedBounds"
            />
        <TextView
            android:layout_width="match_parent"
            android:padding="@dimen/abc_action_bar_icon_vertical_padding_material"
            android:layout_height="50dp"
            android:layout_gravity="center"
            android:id="@+id/txtcharge_status"
            />
        <TextView
            android:layout_width="match_parent"
            android:padding="@dimen/abc_action_bar_icon_vertical_padding_material"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:id="@+id/txtport"/>
    </LinearLayout>
</ScrollView>

 

Gradle Dependency

Add the following dependency in your gradle file

compile 'me.itangqi.waveloadingview:library:0.3.5'

Screen Shot

Working Demo

I hope this tutorial helped you to learn Creating animation with background data (Battery Status) . To get the latest news and updates follow us on twitter facebook, subscribe to our YouTube channel.  And If you have any query then please let us know by using comment form.

Categories: Android
Jamaley Hussain: Hello, I am Jamaley. I did my graduation from StaffordShire University UK . Fortunately, I find myself quite passionate about Computers and Technology.