Statistics App in Android
Difficulty level : Very-Easy * Last Updated On : 20th June, 2021
This is a simple Statistics Application that can be used to track the opening, closing, and force closing of the application.
Below is the picture showing the output of this Project
The Source Code for the above output is as follows: -
To display the imageview on the screen xml code is used.
XML CODE : (file name - activity_main.xml)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
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"
android:background="@color/white"
tools:context=".MainActivity">
<androidx.cardview.widget.CardView
android:id="@+id/cardViewGraph"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="15dp"
android:elevation="10dp"
app:cardCornerRadius="10dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:weightSum="2">
<org.eazegraph.lib.charts.PieChart
android:id="@+id/pieChart"
android:layout_width="0dp"
android:layout_height="match_parent"
android:padding="6dp"
android:layout_weight="1"
android:layout_marginTop="15dp"
android:layout_marginStart="15dp"
android:layout_marginBottom="15dp" />
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:layout_marginStart="20dp"
android:orientation="vertical"
android:gravity="center_vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="20dp"
android:layout_gravity="center_vertical">
<View
android:layout_width="15dp"
android:layout_height="match_parent"
android:background="@color/yellow" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/app_open"
android:paddingStart="10dp"
tools:ignore="RtlSymmetry" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="20dp"
android:layout_gravity="center_vertical"
android:layout_marginTop="5dp">
<View
android:layout_width="15dp"
android:layout_height="match_parent"
android:background="@color/green" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/force_close"
android:paddingStart="10dp"
tools:ignore="RtlSymmetry" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="20dp"
android:layout_gravity="center_vertical"
android:layout_marginTop="5dp">
<View
android:layout_width="15dp"
android:layout_height="match_parent"
android:background="@color/red" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/normal_close"
android:paddingStart="10dp"
tools:ignore="RtlSymmetry" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/cardViewGraph"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="10dp"
android:layout_marginBottom="20dp"
android:elevation="10dp"
app:cardCornerRadius="10dp"
android:id="@+id/details">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/app_statistics"
android:textSize="23sp"
android:textStyle="bold"
android:layout_marginStart="25dp"
android:layout_marginTop="20dp" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:background="@color/color3"
android:layout_marginTop="15dp" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginRight="25dp"
android:layout_marginLeft="25dp"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="sans-serif-light"
android:text="@string/app_open"
android:textSize="15sp"
tools:ignore="RelativeOverlap" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/_0"
android:id="@+id/AppOpen"
android:textAlignment="textEnd"
android:textSize="18sp"
android:textColor="@color/color3"
android:textStyle="bold"
android:fontFamily="sans-serif-light"
android:layout_alignParentEnd="true" />
</RelativeLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@color/color3"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginRight="25dp"
android:layout_marginLeft="25dp"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="sans-serif-light"
android:text="@string/force_app_closing"
android:textSize="15sp"
tools:ignore="RelativeOverlap" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/_0"
android:id="@+id/ForceClosing"
android:textAlignment="textEnd"
android:textSize="18sp"
android:textColor="@color/color3"
android:textStyle="bold"
android:fontFamily="sans-serif-light"
android:layout_alignParentEnd="true" />
</RelativeLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@color/color3"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginRight="25dp"
android:layout_marginLeft="25dp"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="sans-serif-light"
android:text="@string/normal_app_closing"
android:textSize="15sp"
tools:ignore="RelativeOverlap" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/_0"
android:id="@+id/NormalClosing"
android:textAlignment="textEnd"
android:textSize="18sp"
android:textColor="@color/color3"
android:textStyle="bold"
android:fontFamily="sans-serif-light"
android:layout_alignParentEnd="true" />
</RelativeLayout>
</LinearLayout>
</androidx.cardview.widget.CardView>
</RelativeLayout>
Java file is used to write the program and all the actions that are performed on the widget while on the screen is done by the Java file.
MAIN ACTIVITY CODE : (file name - MainActivity.java)
package com.example.differentandroidcodes;
import android.annotation.SuppressLint;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Bundle;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import org.eazegraph.lib.charts.PieChart;
import org.eazegraph.lib.models.PieModel;
@SuppressLint("SetTextI18n")
public class MainActivity extends AppCompatActivity {
TextView tvOpen, tvForceClosed, tvNormalClose;
PieChart pieChart;
String appOpened, finalAppOpened, appClosed, finalAppClosed, finalForceAppClosed, appOpen;
int i, j, k;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().setStatusBarColor(getResources().getColor(R.color.color3));
setContentView(R.layout.activity_main);
SharedPreferences getShared2 = getSharedPreferences("statistics", MODE_PRIVATE);
appOpen = getShared2.getString("appOpened", null);
if (appOpen != null) {
i = Integer.parseInt(appOpen);
i = i + 1;
appOpen = Integer.toString(i);
SharedPreferences shard = getSharedPreferences("statistics", MODE_PRIVATE);
final SharedPreferences.Editor editor = shard.edit();
editor.putString("appOpened", appOpen);
editor.apply();
} else {
SharedPreferences shard = getSharedPreferences("statistics", MODE_PRIVATE);
final SharedPreferences.Editor editor = shard.edit();
editor.putString("appClosed", "0");
editor.putString("appOpened", "1");
editor.apply();
}
SharedPreferences getShared = getSharedPreferences("statistics", MODE_PRIVATE);
appOpened = getShared.getString("appOpened", null);
appClosed = getShared.getString("appClosed", null);
if (appOpened != null || appClosed != null) {
finalAppOpened = appOpened;
finalAppClosed = appClosed;
i = Integer.parseInt(finalAppOpened);
j = Integer.parseInt(finalAppClosed);
} else {
assert false;
i = Integer.parseInt(appOpened);
j = 0;
}
k = (i - j) - 1;
finalForceAppClosed = Integer.toString(k);
tvOpen = findViewById(R.id.AppOpen);
tvForceClosed = findViewById(R.id.ForceClosing);
tvNormalClose = findViewById(R.id.NormalClosing);
pieChart = findViewById(R.id.pieChart);
setData();
}
private void setData() {
// Set the percentage of language used
tvOpen.setText(finalAppOpened);
tvForceClosed.setText(finalForceAppClosed);
tvNormalClose.setText(finalAppClosed);
// Set the data and color to the pie chart
pieChart.addPieSlice(
new PieModel(
"App Opened",
Integer.parseInt(finalAppOpened),
Color.parseColor("#FFA726")));
pieChart.addPieSlice(
new PieModel(
"Python",
Integer.parseInt(finalForceAppClosed),
Color.parseColor("#66BB6A")));
pieChart.addPieSlice(
new PieModel(
"C++",
Integer.parseInt(finalAppClosed),
Color.parseColor("#EF5350")));
// To animate the pie chart
pieChart.startAnimation();
}
@Override
public void onBackPressed() {
super.onBackPressed();
String appClosed;
int i;
SharedPreferences getShared = getSharedPreferences("statistics", MODE_PRIVATE);
appClosed = getShared.getString("appClosed", null);
if (appClosed != null) {
i = Integer.parseInt(appClosed);
i = i + 1;
appClosed = Integer.toString(i);
SharedPreferences shard = getSharedPreferences("statistics", MODE_PRIVATE);
final SharedPreferences.Editor editor = shard.edit();
editor.putString("appClosed", appClosed);
editor.apply();
} else {
SharedPreferences shard = getSharedPreferences("statistics", MODE_PRIVATE);
final SharedPreferences.Editor editor = shard.edit();
editor.putString("appClosed", "1");
editor.apply();
}
finishAffinity();
}
}
Note : SharedPreferences are used to store the data of the application in the local storage of the mobile devices.
Manifest file is generated automatically. You can view this file by clicking on the manifest option that is show the project structure image.
MANIFEST FILE CODE: (file name - AndroidManifest.xml)
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.example.differentandroidcodes">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.DifferentStyleButtons"
tools:ignore="AllowBackup">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Output : -
Light Theme
Dark Theme
Dark Theme
Project Structure
Note : The Selected Files are the important files and the code of those files are given in this article.
0 Comments