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