I’m working on my first Android app for work time tracking.
MainActivity.java:
package com.example;
import com.example.work.R;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
public class MainActivity extends AppCompatActivity {
private TextView startTimeTextView, endTimeTextView, todayTimeTextView, weekTimeTextView;
private Button startWorkButton, endWorkButton, resetWeekButton, showHelpButton;
private boolean working = false;
private long startTime = 0, endTime = 0;
private float[] workTimes = new float[7]; // Array to store weekly work times
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
startTimeTextView = findViewById(R.id.startTimeTextView);
endTimeTextView = findViewById(R.id.endTimeTextView);
todayTimeTextView = findViewById(R.id.todayTimeTextView);
weekTimeTextView = findViewById(R.id.weekTimeTextView);
startWorkButton = findViewById(R.id.startWorkButton);
endWorkButton = findViewById(R.id.endWorkButton);
resetWeekButton = findViewById(R.id.resetWeekButton);
showHelpButton = findViewById(R.id.showHelpButton);
startWorkButton.setOnClickListener(view -> startWork());
endWorkButton.setOnClickListener(view -> endWork());
resetWeekButton.setOnClickListener(view -> resetWeek());
showHelpButton.setOnClickListener(view -> showHelp());
loadWorkTimes(); // Load work times from storage
updateUI(); // Update UI with loaded data
}
private void startWork() {
if (working) {
startTime = System.currentTimeMillis(); // Record start time in milliseconds
startTimeTextView.setText("Start Time: " + formatTime(startTime));
} else {
startTime = System.currentTimeMillis(); // Begin initial work session
startTimeTextView.setText("Start Time: " + formatTime(startTime));
endTime = 0; // Reset end time on new session
endTimeTextView.setText("End Time: ");
}
working = true; // Set working status to true
saveWorkingStatus(); // Save working status
updateUI();
}
private void endWork() {
if (!working) {
endTimeTextView.setText("You haven't started working yet!");
return;
}
endTime = System.currentTimeMillis(); // Record end time in milliseconds
float workDuration = (endTime - startTime) / 3600000.0f; // Convert milliseconds to hours
int dayOfWeek = getDayOfWeek();
workTimes[dayOfWeek] += workDuration; // Add work duration to the respective day
saveWorkTimes(); // Save updated work times
updateUI();
working = false; // Reset working status
saveWorkingStatus(); // Save working status
endTimeTextView.setText("End Time: " + formatTime(endTime));
}
private void resetWeek() {
for (int i = 0; i < workTimes.length; i++) {
workTimes[i] = 0; // Reset work times to zero
}
saveWorkTimes(); // Save reset work times
updateUI();
}
private void showHelp() {
// Show help dialog or message
new android.app.AlertDialog.Builder(this)
.setTitle("Help")
.setMessage("All commands:n- Start Work: Start working session.n- End Work: End working session.n- Reset Week: Reset weekly work times.")
.setPositiveButton("OK", null)
.show();
}
private void updateUI() {
StringBuilder weekText = new StringBuilder();
weekText.append("Today: ").append(String.format(Locale.US, "%.3fh", workTimes[getDayOfWeek()])).append("n");
float weekTotal = 0;
for (int i = 0; i < 7; i++) {
weekText.append("Day ").append(i).append(": ").append(String.format(Locale.US, "%.3fh", workTimes[i])).append("n");
weekTotal += workTimes[i];
}
weekText.append("Week: ").append(String.format(Locale.US, "%.3fh", weekTotal)).append("n");
weekTimeTextView.setText(weekText.toString());
if (startTime > 0) {
startTimeTextView.setText("Start Time: " + formatTime(startTime));
}
if (endTime > 0) {
endTimeTextView.setText("End Time: " + formatTime(endTime));
}
}
private String formatTime(long timeInMillis) {
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm", Locale.US);
return sdf.format(new Date(timeInMillis));
}
private int getDayOfWeek() {
return (int) ((System.currentTimeMillis() / (1000 * 60 * 60 * 24)) % 7); // Calculate day of week from milliseconds
}
private void saveWorkTimes() {
getSharedPreferences("workTimesPrefs", MODE_PRIVATE).edit().putString("workTimes", serializeWorkTimes()).apply();
}
private String serializeWorkTimes() {
StringBuilder sb = new StringBuilder();
for (float time : workTimes) {
sb.append(time).append(",");
}
return sb.toString();
}
private void loadWorkTimes() {
String workTimesString = getSharedPreferences("workTimesPrefs", MODE_PRIVATE).getString("workTimes", "0,0,0,0,0,0,0");
String[] workTimesArray = workTimesString.split(",");
for (int i = 0; i < workTimesArray.length; i++) {
workTimes[i] = Float.parseFloat(workTimesArray[i]);
}
}
private void saveWorkingStatus() {
getSharedPreferences("workingPrefs", MODE_PRIVATE).edit().putBoolean("working", working).apply();
}
}
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp">
<TextView
android:id="@+id/titleTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Work Time Tracker"
android:textSize="24sp"
android:layout_centerHorizontal="true"
android:layout_marginBottom="16dp"/>
<TextView
android:id="@+id/startTimeTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Start Time: "
android:layout_below="@+id/titleTextView"
android:layout_marginBottom="8dp"
android:layout_centerHorizontal="true"/>
<TextView
android:id="@+id/endTimeTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="End Time: "
android:layout_below="@+id/startTimeTextView"
android:layout_marginBottom="8dp"
android:layout_centerHorizontal="true"/>
<TextView
android:id="@+id/todayTimeTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Today: 0.000h"
android:layout_below="@+id/endTimeTextView"
android:layout_marginBottom="8dp"
android:layout_centerHorizontal="true"/>
<TextView
android:id="@+id/weekTimeTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Week: 0.000h"
android:layout_below="@+id/todayTimeTextView"
android:layout_centerHorizontal="true"/>
<Button
android:id="@+id/startWorkButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Start Work"
android:layout_below="@+id/weekTimeTextView"
android:layout_marginTop="16dp"
android:layout_centerHorizontal="true"/>
<Button
android:id="@+id/endWorkButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="End Work"
android:layout_below="@+id/startWorkButton"
android:layout_marginTop="8dp"
android:layout_centerHorizontal="true"/>
<Button
android:id="@+id/resetWeekButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Reset Week"
android:layout_below="@+id/endWorkButton"
android:layout_marginTop="8dp"
android:layout_centerHorizontal="true"/>
<Button
android:id="@+id/showHelpButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Help"
android:layout_below="@+id/resetWeekButton"
android:layout_marginTop="8dp"
android:layout_centerHorizontal="true"/>
</RelativeLayout>
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">
<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.Work"
tools:targetApi="31" />
</manifest>
I don’t get any errors and I am able to successfully build apk file. However when I install it on my Android device I don’t see option to open it. App is installed correctly and I can even find it inside settings->apps…
Folder structure
app/java/com.example/work/MainActivity.java
app/res/layout/activity_main.xml
app/manifests/AndroidManifest.xml
Could you please help me to fix this? I have never made an app with Android Studio, so everything is new to me? I would be very thankful.