Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .idea/AndroidProjectSystem.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/compiler.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions .idea/deploymentTargetSelector.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 19 additions & 0 deletions .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/kotlinc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions .idea/migrations.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions .idea/runConfigurations.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions app/src/main/java/com/example/bcsd_android_2025_1/ListData.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package com.example.bcsd_android_2025_1

data class ListData(val time : String)
88 changes: 85 additions & 3 deletions app/src/main/java/com/example/bcsd_android_2025_1/MainActivity.kt
Original file line number Diff line number Diff line change
@@ -1,14 +1,96 @@
package com.example.bcsd_android_2025_1

import android.os.Bundle
import androidx.activity.enableEdgeToEdge
import android.os.SystemClock
import android.widget.Button
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.lifecycle.lifecycleScope
import kotlinx.coroutines.Job
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch

class MainActivity : AppCompatActivity() {
private lateinit var timeTextView: TextView
private lateinit var startPauseButton:Button
private lateinit var stopButton:Button
private lateinit var lapButton:Button
private lateinit var recyclerViewAdapter: RecyclerViewAdapter
private lateinit var recyclerView:RecyclerView

private var isRunning = false
private var startTime = 0L
private var pauseOffset = 0L
private var timerJob: Job? = null


override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

timeTextView = findViewById(R.id.time_textview)
startPauseButton = findViewById(R.id.start_pause_toggle_button)
stopButton = findViewById(R.id.stop_button)
lapButton = findViewById(R.id.lap_button)
recyclerView = findViewById(R.id.recycler_view)
recyclerViewAdapter = RecyclerViewAdapter()
recyclerView.adapter = recyclerViewAdapter
recyclerView.layoutManager = LinearLayoutManager(this)

startPauseButton.setOnClickListener {
if(!isRunning){
startPauseButton.text = getString(R.string.pause_text)
startTime = SystemClock.elapsedRealtime() - pauseOffset
startTimer()
isRunning=true
}else{
startPauseButton.text=getString(R.string.start_text)
pauseOffset = SystemClock.elapsedRealtime() - startTime
stopTimer()
isRunning=false
}
}

stopButton.setOnClickListener {
stopTimer()
timeTextView.text = getString(R.string.default_time_text)
startPauseButton.text = getString(R.string.start_text)
isRunning = false
pauseOffset = 0L
recyclerViewAdapter.removeAllItems()
}

lapButton.setOnClickListener {
if(isRunning){
val elapsed = SystemClock.elapsedRealtime() - startTime
val min = (elapsed / 1000) / 60
val sec = (elapsed / 1000) % 60
val mil = (elapsed % 1000) / 10
val nowTime = getString(R.string.time_text, min, sec, mil)

recyclerViewAdapter.addLapItem(nowTime)
recyclerView.scrollToPosition(0)
}
}
}

private fun startTimer(){
timerJob = lifecycleScope.launch{
while(true){
val elapsed = SystemClock.elapsedRealtime()-startTime
val min = (elapsed/1000)/60
val sec = (elapsed/1000)%60
val mil = (elapsed%1000)/10
timeTextView.text = getString(R.string.time_text, min, sec, mil)
delay(10L)
}
}
}

private fun stopTimer(){
timerJob?.cancel()
timerJob = null
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.example.bcsd_android_2025_1

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView

class RecyclerViewAdapter:RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder>(){
private val items = mutableListOf<ListData>()

inner class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
private val lapItemTextView: TextView = view.findViewById(R.id.lap_item_textview)

fun bind(item: ListData) {
lapItemTextView.text = item.time
}
}

fun addLapItem(time:String){
items.add(0, ListData(time))
notifyItemInserted(0)
}

fun removeAllItems(){
items.clear()
notifyDataSetChanged()
}

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val inflatedView = LayoutInflater.from(parent.context).inflate(R.layout.item_recyclerview, parent, false)
return ViewHolder(inflatedView)
}

override fun getItemCount(): Int{
return items.size
}

override fun onBindViewHolder(holder: ViewHolder, position: Int) {
holder.bind(items[position])
}
}
53 changes: 50 additions & 3 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,59 @@
tools:context=".MainActivity">

<TextView
android:id="@+id/time_textview"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:gravity="center"
android:text="@string/default_time_text"
android:textSize="50sp"
app:layout_constraintVertical_bias="0.1"
android:textColor="@color/black"
android:textStyle="bold"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBottom_toBottomOf="parent"/>

<Button
android:id="@+id/start_pause_toggle_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/start_text"
app:layout_constraintEnd_toStartOf="@id/stop_button"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@id/stop_button"/>

<Button
android:id="@+id/stop_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/stop_text"
android:layout_marginTop="30dp"
app:layout_constraintTop_toBottomOf="@id/time_textview"
app:layout_constraintStart_toEndOf="@id/start_pause_toggle_button"
app:layout_constraintEnd_toStartOf="@id/lap_button"/>

<Button
android:id="@+id/lap_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello Android!"
android:text="@string/lap_text"
app:layout_constraintTop_toTopOf="@id/stop_button"
app:layout_constraintStart_toEndOf="@id/stop_button"
app:layout_constraintEnd_toEndOf="parent"/>

<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginTop="20dp"
android:layout_marginStart="10dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toBottomOf="@id/stop_button"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
tools:listitem="@layout/item_recyclerview"/>

</androidx.constraintlayout.widget.ConstraintLayout>
19 changes: 19 additions & 0 deletions app/src/main/res/layout/item_recyclerview.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/layout_item_recycler_view"
android:layout_marginBottom="5dp">

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/lap_item_textview"
android:textSize="20sp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:textColor="@color/black"/>

</androidx.constraintlayout.widget.ConstraintLayout>
6 changes: 6 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
<resources>
<string name="app_name">BCSD_Android_2025-1</string>
<string name="start_text">START</string>
<string name="pause_text">PAUSE</string>
<string name="stop_text">STOP</string>
<string name="lap_text">LAP</string>
<string name="default_time_text">00:00:00</string>
<string name="time_text">%02d:%02d:%02d</string>
</resources>