integrated start/stop button to ready or stopped button
This commit is contained in:
@@ -6,7 +6,6 @@ import android.media.AudioAttributes
|
||||
import android.media.SoundPool
|
||||
import android.os.Bundle
|
||||
import android.os.Handler
|
||||
import android.widget.Button
|
||||
import android.widget.EditText
|
||||
import android.widget.TextView
|
||||
|
||||
@@ -26,7 +25,6 @@ class MainActivity : Activity() {
|
||||
|
||||
val status = findViewById<TextView>(R.id.statusText)
|
||||
val input = findViewById<EditText>(R.id.timerInput)
|
||||
val btn = findViewById<Button>(R.id.mainBtn)
|
||||
|
||||
val attrs = AudioAttributes.Builder()
|
||||
.setUsage(AudioAttributes.USAGE_MEDIA)
|
||||
@@ -40,64 +38,59 @@ class MainActivity : Activity() {
|
||||
|
||||
soundId = soundPool.load(this, R.raw.sound, 1)
|
||||
|
||||
// Initial: Start-Zustand (grün)
|
||||
btn.setBackgroundColor(Color.GREEN)
|
||||
btn.setTextColor(Color.BLACK)
|
||||
status.setBackgroundColor(Color.LTGRAY)
|
||||
status.setTextColor(Color.BLACK)
|
||||
// Anfangszustand
|
||||
updateUI(status, "Ready", Color.LTGRAY)
|
||||
|
||||
btn.setOnClickListener {
|
||||
// Das Statusfeld ist jetzt klickbar
|
||||
status.setOnClickListener {
|
||||
if (isPlaying) {
|
||||
stopSound(btn, status)
|
||||
stopSound(status)
|
||||
} else {
|
||||
val min = input.text.toString().toIntOrNull() ?: 0
|
||||
startSound(min, btn, status)
|
||||
startSound(min, status)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun startSound(minutes: Int, btn: Button, status: TextView) {
|
||||
stopSound(btn, status) // sicherstellen, dass nichts doppelt läuft
|
||||
private fun startSound(minutes: Int, status: TextView) {
|
||||
stopSound(status)
|
||||
streamId = soundPool.play(soundId, 1f, 1f, 1, -1, 1f)
|
||||
isPlaying = true
|
||||
btn.text = "Stop"
|
||||
btn.setBackgroundColor(Color.RED)
|
||||
btn.setTextColor(Color.BLACK)
|
||||
|
||||
status.setBackgroundColor(Color.GREEN)
|
||||
status.text = if (minutes > 0) "Läuft: ${minutes} min" else "Läuft: ∞"
|
||||
|
||||
if (minutes > 0) {
|
||||
remainingMinutes = minutes
|
||||
updateUI(status, "Runs for: $remainingMinutes min", Color.GREEN)
|
||||
countdownRunnable = object : Runnable {
|
||||
override fun run() {
|
||||
remainingMinutes--
|
||||
if (remainingMinutes > 0) {
|
||||
status.text = "Läuft: ${remainingMinutes} min"
|
||||
updateUI(status, "Runs for: $remainingMinutes min", Color.GREEN)
|
||||
handler.postDelayed(this, 60_000L)
|
||||
} else {
|
||||
stopSound(btn, status)
|
||||
stopSound(status)
|
||||
}
|
||||
}
|
||||
}
|
||||
handler.postDelayed(countdownRunnable!!, 60_000L)
|
||||
} else {
|
||||
updateUI(status, "Runs: ∞", Color.GREEN)
|
||||
}
|
||||
}
|
||||
|
||||
private fun stopSound(btn: Button, status: TextView) {
|
||||
private fun stopSound(status: TextView) {
|
||||
if (isPlaying) {
|
||||
soundPool.stop(streamId)
|
||||
isPlaying = false
|
||||
}
|
||||
remainingMinutes = 0
|
||||
handler.removeCallbacks(countdownRunnable ?: Runnable { })
|
||||
status.text = "Gestoppt"
|
||||
status.setBackgroundColor(Color.RED)
|
||||
status.setTextColor(Color.BLACK)
|
||||
updateUI(status, "Stopped", Color.RED)
|
||||
}
|
||||
|
||||
btn.text = "Start"
|
||||
btn.setBackgroundColor(Color.GREEN)
|
||||
btn.setTextColor(Color.BLACK)
|
||||
private fun updateUI(view: TextView, text: String, color: Int) {
|
||||
view.text = text
|
||||
view.setBackgroundColor(color)
|
||||
view.setTextColor(Color.BLACK)
|
||||
}
|
||||
|
||||
override fun onDestroy() {
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:gravity="center"
|
||||
android:orientation="vertical"
|
||||
android:padding="20dp">
|
||||
|
||||
@@ -18,55 +19,22 @@
|
||||
<Space
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="50dp" />
|
||||
<View
|
||||
android:id="@+id/statusCircle"
|
||||
android:layout_width="80dp"
|
||||
android:layout_height="80dp"
|
||||
android:layout_gravity="center"
|
||||
android:background="@drawable/circle_gray" />
|
||||
|
||||
<TextView
|
||||
android:id="@+id/statusText"
|
||||
android:gravity="center_horizontal"
|
||||
android:text="Bereit"
|
||||
android:textSize="24sp"
|
||||
android:textSize="26sp"
|
||||
android:gravity="center"
|
||||
android:textColor="#000000"
|
||||
android:padding="12dp"
|
||||
android:padding="24dp"
|
||||
android:background="#CCCCCC"
|
||||
android:layout_marginBottom="20dp"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content" />
|
||||
|
||||
<TextView
|
||||
android:id="@+id/Info"
|
||||
android:gravity="center"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="Timer in Minutes:"
|
||||
android:textSize="14sp" />
|
||||
<Space
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="15dip" />
|
||||
|
||||
<EditText
|
||||
android:id="@+id/timerInput"
|
||||
android:hint="empty means endless"
|
||||
android:hint="Sleep Timer (Minuten)"
|
||||
android:inputType="number"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"/>
|
||||
<Space
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="30dip" />
|
||||
|
||||
<Button
|
||||
android:id="@+id/mainBtn"
|
||||
android:text="Start"
|
||||
android:textColor="#000000"
|
||||
android:background="#00FF00"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginTop="10dp"
|
||||
android:textSize="34sp"
|
||||
/>
|
||||
|
||||
android:layout_marginTop="20dp"/>
|
||||
</LinearLayout>
|
||||
|
||||
Reference in New Issue
Block a user