I am new to coding and most of my coding and unoptimized but I come straight to the point:
When I start the activity the progressBars should fill up to the calculated fill status. They do but the progressbar for 20 Minutes and 2 hours fill up nearly instant and the longer the progressBar durantion the longer it takes to animate the fill status. I want them all to fill up to the calculated fill status over the time of 2 seconds. So if one bar is filled 100% and another is filled 30 percent the should reach 100 percent and 30% at the same time. I add my xml code in case you need it.
Thank you in advance. I am tired of chatgpt giving me repeatedly the same 3 wrong options.
package com.fltg.frei3
import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.widget.Button
import android.widget.ProgressBar
import android.widget.TextView
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import com.google.android.gms.ads.AdRequest
import com.google.android.gms.ads.AdView
import java.util.*
import android.animation.ValueAnimator
class SecondActivity : AppCompatActivity() {
private lateinit var timeTextView: TextView
private lateinit var backButton: Button
private lateinit var progressBar1: ProgressBar
private lateinit var progressBar2: ProgressBar
private lateinit var progressBar3: ProgressBar
private lateinit var progressBar4: ProgressBar
private lateinit var progressBar5: ProgressBar
private lateinit var progressBar6: ProgressBar
private lateinit var progressBar7: ProgressBar
private lateinit var progressBar8: ProgressBar
private lateinit var progressBar9: ProgressBar
private lateinit var progressBar10: ProgressBar
private lateinit var progressBar11: ProgressBar
private lateinit var progressBar12: ProgressBar
private lateinit var progressBar13: ProgressBar
private lateinit var handler: Handler
private lateinit var timer: Timer
private var elapsedTimeInMilliseconds: Long = 0
private lateinit var adView: AdView
private lateinit var backgroundChange: BackgroundChange
private var progress1 = 0
private var progress2 = 0
private var progress3 = 0
private var progress4 = 0
private var progress5 = 0
private var progress6 = 0
private var progress7 = 0
private var progress8 = 0
private var progress9 = 0
private var progress10 = 0
private var progress11 = 0
private var progress12 = 0
private var progress13 = 0
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_second)
timeTextView = findViewById(R.id.timeTextView)
backButton = findViewById(R.id.backButton)
progressBar1 = findViewById(R.id.progressBar1)
progressBar2 = findViewById(R.id.progressBar2)
progressBar3 = findViewById(R.id.progressBar3)
progressBar4 = findViewById(R.id.progressBar4)
progressBar5 = findViewById(R.id.progressBar5)
progressBar6 = findViewById(R.id.progressBar6)
progressBar7 = findViewById(R.id.progressBar7)
progressBar8 = findViewById(R.id.progressBar8)
progressBar9 = findViewById(R.id.progressBar9)
progressBar10 = findViewById(R.id.progressBar10)
progressBar11 = findViewById(R.id.progressBar11)
progressBar12 = findViewById(R.id.progressBar12)
progressBar13 = findViewById(R.id.progressBar13)
handler = Handler(Looper.getMainLooper())
adView = findViewById(R.id.adView)
// Initialize the timer
timer = Timer()
backButton.setOnClickListener {
if (::timer.isInitialized) {
timer.cancel() // Stop the timer when going back
}
finish()
}
val savedDateTime = intent.getStringExtra("savedDateTime") ?: ""
if (savedDateTime.isNotEmpty()) {
elapsedTimeInMilliseconds = DateTimeHelper.calculateElapsedTimeInMilliseconds(savedDateTime)
startTimer()
}
val adRequest = AdRequest.Builder().build()
adView.loadAd(adRequest)
// Initialize the BackgroundChange class
backgroundChange = application as BackgroundChange
// Get the selected gradient from SharedPreferences and set the background
val selectedGradient = backgroundChange.getSelectedGradient()
setActivityBackground(selectedGradient)
}
private fun animateProgressBar(progressBar: ProgressBar, targetProgress: Int) {
val animationDuration = 2000L // 2000 milliseconds
val animator = ValueAnimator.ofInt(progressBar.progress, targetProgress)
animator.duration = animationDuration
// Update the progress as the animation progresses
animator.addUpdateListener { animation ->
val progress = animation.animatedValue as Int
progressBar.progress = progress
}
animator.start()
}
private fun startTimer() {
timer.scheduleAtFixedRate(object : TimerTask() {
override fun run() {
elapsedTimeInMilliseconds += 100
updateProgressBars()
handler.post {
// Additional updates when the timer is running
}
}
}, 0, 10000) // Adjust the update interval as needed
}
private fun updateProgressBars() {
// Set the maximum duration for each progress bar
val maxDuration1 = 1_200_000 // 20 minutes
val maxDuration2 = 7_200_000 // 2 hours
val maxDuration3 = 86_400_000 // 24 hours
val maxDuration4 = 172_800_000 // 48 hours
val maxDuration5 = 259_200_000 // 72 hours
val maxDuration6 = 1_209_600_000 // 2 weeks
val maxDuration7 = 2_628_000_000 // 1 month
val maxDuration8 = 7_884_000_000 // 3 months
val maxDuration9 = 15_768_000_000 // 6 months
val maxDuration10 = 31_536_000_000 // 1 year
val maxDuration11 = 157_680_000_000 // 5 years
val maxDuration12 = 315_360_000_000 // 10 years
val maxDuration13 = 473_040_000_000 // 15 years
// Calculate the progress for each progress bar
progress1 = (elapsedTimeInMilliseconds.toFloat() / maxDuration1 * 100).toInt()
progress2 = (elapsedTimeInMilliseconds.toFloat() / maxDuration2 * 100).toInt()
progress3 = (elapsedTimeInMilliseconds.toFloat() / maxDuration3 * 100).toInt()
progress4 = (elapsedTimeInMilliseconds.toFloat() / maxDuration4 * 100).toInt()
progress5 = (elapsedTimeInMilliseconds.toFloat() / maxDuration5 * 100).toInt()
progress6 = (elapsedTimeInMilliseconds.toFloat() / maxDuration6 * 100).toInt()
progress7 = (elapsedTimeInMilliseconds.toFloat() / maxDuration7 * 100).toInt()
progress8 = (elapsedTimeInMilliseconds.toFloat() / maxDuration8 * 100).toInt()
progress9 = (elapsedTimeInMilliseconds.toFloat() / maxDuration9 * 100).toInt()
progress10 = (elapsedTimeInMilliseconds.toFloat() / maxDuration10 * 100).toInt()
progress11 = (elapsedTimeInMilliseconds.toFloat() / maxDuration11 * 100).toInt()
progress12 = (elapsedTimeInMilliseconds.toFloat() / maxDuration12 * 100).toInt()
progress13 = (elapsedTimeInMilliseconds.toFloat() / maxDuration13 * 100).toInt()
// Update all progress bars
runOnUiThread {
animateProgressBar(progressBar1, progress1)
animateProgressBar(progressBar2, progress2)
animateProgressBar(progressBar3, progress3)
animateProgressBar(progressBar4, progress4)
animateProgressBar(progressBar5, progress5)
animateProgressBar(progressBar6, progress6)
animateProgressBar(progressBar7, progress7)
animateProgressBar(progressBar8, progress8)
animateProgressBar(progressBar9, progress9)
animateProgressBar(progressBar10, progress10)
animateProgressBar(progressBar11, progress11)
animateProgressBar(progressBar12, progress12)
animateProgressBar(progressBar13, progress13)
}
}
private fun setActivityBackground(selectedGradient: Int) {
val backgroundResourceId = when (selectedGradient) {
1 -> R.drawable.backgroundgradient
2 -> R.drawable.backgroundgradient2
3 -> R.drawable.backgroundgradient3
4 -> R.drawable.backgroundgradient4
5 -> R.drawable.backgroundgradient5
6 -> R.drawable.backgroundgradient6
else -> R.drawable.default_background
}
findViewById<View>(android.R.id.content).setBackgroundResource(backgroundResourceId)
}
override fun onDestroy() {
super.onDestroy()
timer.cancel() // Stop the timer when the activity is destroyed
}
}
XML:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
tools:context=".SecondActivity">
<ScrollView
android:id="@+id/scrollView"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginBottom="5dp"
app:layout_constraintBottom_toTopOf="@+id/adView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/headingTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Gesundheitsfortschritt"
android:textSize="20sp"
android:textStyle="bold"
android:textColor="#FFFFFF"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="16dp" />
<TextView
android:id="@+id/timeTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="20 Minuten:\nHerzfrequenz wie Nichtraucher\nKörpertemperatur wie Nichtraucher"
android:textSize="16sp"
android:textColor="#FFFFFF"
app:layout_constraintTop_toBottomOf="@+id/headingTextView"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:layout_marginTop="16dp"
android:gravity="center"
/>
<ProgressBar
android:id="@+id/progressBar1"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="0dp"
android:layout_height="10dp"
android:layout_marginStart="45dp"
android:layout_marginTop="1dp"
android:layout_marginEnd="45dp"
android:max="100"
android:progressTint="#FFFFFF"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/timeTextView" />
<TextView
android:id="@+id/timeTextView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="2 Stunden:\nSauerstofftransport normalisiert"
android:textSize="16sp"
android:textColor="#FFFFFF"
app:layout_constraintTop_toBottomOf="@+id/progressBar1"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:layout_marginTop="16dp"
android:gravity="center"
/>
<ProgressBar
android:id="@+id/progressBar2"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="0dp"
android:layout_height="10dp"
android:layout_marginStart="45dp"
android:layout_marginTop="1dp"
android:layout_marginEnd="45dp"
android:max="100"
android:progressTint="#FFFFFF"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/timeTextView2" />
<TextView
android:id="@+id/timeTextView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="24 Stunden:\nHerzinfarktrisiko sinkt bereits"
android:textSize="16sp"
android:textColor="#FFFFFF"
app:layout_constraintTop_toBottomOf="@+id/progressBar2"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:layout_marginTop="16dp"
android:gravity="center"
/>
<ProgressBar
android:id="@+id/progressBar3"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="0dp"
android:layout_height="10dp"
android:layout_marginStart="45dp"
android:layout_marginTop="1dp"
android:layout_marginEnd="45dp"
android:max="100"
android:progressTint="#FFFFFF"
app:layout_constraintTop_toBottomOf="@+id/timeTextView3"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
<TextView
android:id="@+id/timeTextView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="2 Tage:\nGeruchs- und Geschmackssinn sensibler"
android:textSize="16sp"
android:textColor="#FFFFFF"
app:layout_constraintTop_toBottomOf="@+id/progressBar3"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:layout_marginTop="16dp"
android:gravity="center"
/>
<ProgressBar
android:id="@+id/progressBar4"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="0dp"
android:layout_height="10dp"
android:layout_marginStart="45dp"
android:layout_marginTop="1dp"
android:layout_marginEnd="45dp"
android:max="100"
android:progressTint="#FFFFFF"
app:layout_constraintTop_toBottomOf="@+id/timeTextView4"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
<TextView
android:id="@+id/timeTextView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="3 Tage:\nAtmen wird leichter\nBronchien entspannen sich"
android:textSize="16sp"
android:textColor="#FFFFFF"
app:layout_constraintTop_toBottomOf="@+id/progressBar4"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:layout_marginTop="16dp"
android:gravity="center"
/>
<ProgressBar
android:id="@+id/progressBar5"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="0dp"
android:layout_height="10dp"
android:layout_marginStart="45dp"
android:layout_marginTop="1dp"
android:layout_marginEnd="45dp"
android:max="100"
android:progressTint="#FFFFFF"
app:layout_constraintTop_toBottomOf="@+id/timeTextView5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
<TextView
android:id="@+id/timeTextView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="2 Wochen:\nbessere Durchblutung\nStabilerer Kreislauf"
android:textSize="16sp"
android:textColor="#FFFFFF"
app:layout_constraintTop_toBottomOf="@+id/progressBar5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:layout_marginTop="16dp"
android:gravity="center"
/>
<ProgressBar
android:id="@+id/progressBar6"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="0dp"
android:layout_height="10dp"
android:layout_marginStart="45dp"
android:layout_marginTop="1dp"
android:layout_marginEnd="45dp"
android:max="100"
android:progressTint="#FFFFFF"
app:layout_constraintTop_toBottomOf="@+id/timeTextView6"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
<TextView
android:id="@+id/timeTextView7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1 Monat:\nHustenanfälle gehen zurück\nAbgeschlagenheit geht zurück\nKurzatmigkeit geht zurück"
android:textSize="16sp"
android:textColor="#FFFFFF"
app:layout_constraintTop_toBottomOf="@+id/progressBar6"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:layout_marginTop="16dp"
android:gravity="center"
/>
<ProgressBar
android:id="@+id/progressBar7"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="0dp"
android:layout_height="10dp"
android:layout_marginStart="45dp"
android:layout_marginTop="1dp"
android:layout_marginEnd="45dp"
android:max="100"
android:progressTint="#FFFFFF"
app:layout_constraintTop_toBottomOf="@+id/timeTextView7"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
<TextView
android:id="@+id/timeTextView8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="3 Monate:\nLugenkapazität bis zu 30 Prozent gesteigert\nBronchien reinigen sich"
android:textSize="16sp"
android:textColor="#FFFFFF"
app:layout_constraintTop_toBottomOf="@+id/progressBar7"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:layout_marginTop="16dp"
android:gravity="center"
/>
<ProgressBar
android:id="@+id/progressBar8"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="0dp"
android:layout_height="10dp"
android:layout_marginStart="45dp"
android:layout_marginTop="1dp"
android:layout_marginEnd="45dp"
android:max="100"
android:progressTint="#FFFFFF"
app:layout_constraintTop_toBottomOf="@+id/timeTextView8"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
<TextView
android:id="@+id/timeTextView9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:gravity="center"
android:text="6 Monate:\nLunge allmählich gereinigt\nSchleim abgebaut\nNasennebenhöhlen Verstopfung geht zurück\nInfektionsgefahr verringert"
android:textColor="#FFFFFF"
android:textColorLink="#FFFFFF"
android:textSize="16sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/progressBar8" />
<ProgressBar
android:id="@+id/progressBar9"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="0dp"
android:layout_height="10dp"
android:layout_marginStart="45dp"
android:layout_marginTop="1dp"
android:layout_marginEnd="45dp"
android:max="100"
android:progressTint="#FFFFFF"
app:layout_constraintTop_toBottomOf="@+id/timeTextView9"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
<TextView
android:id="@+id/timeTextView10"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1 Jahr:\nRisiko koronare Herzkrankheit halbiert\nHerz-Kreislaufsystem normalisiert sich"
android:textSize="16sp"
android:textColor="#FFFFFF"
app:layout_constraintTop_toBottomOf="@+id/progressBar9"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:layout_marginTop="16dp"
android:gravity="center"
/>
<ProgressBar
android:id="@+id/progressBar10"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="0dp"
android:layout_height="10dp"
android:layout_marginStart="45dp"
android:layout_marginTop="1dp"
android:layout_marginEnd="45dp"
android:max="100"
android:progressTint="#FFFFFF"
app:layout_constraintTop_toBottomOf="@+id/timeTextView10"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
<TextView
android:id="@+id/timeTextView11"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="5 Jahre:\nHerzinfarktrisiko wie Nichtraucher\nGebärmutterhalskrebs wie Nichtraucher"
android:textSize="16sp"
android:textColor="#FFFFFF"
app:layout_constraintTop_toBottomOf="@+id/progressBar10"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:layout_marginTop="16dp"
android:gravity="center"
/>
<ProgressBar
android:id="@+id/progressBar11"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="0dp"
android:layout_height="10dp"
android:layout_marginStart="45dp"
android:layout_marginTop="1dp"
android:layout_marginEnd="45dp"
android:max="100"
android:progressTint="#FFFFFF"
app:layout_constraintTop_toBottomOf="@+id/timeTextView11"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
<TextView
android:id="@+id/timeTextView12"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="10 Jahre:\nLungenkrebsrisiko halbiert"
android:textSize="16sp"
android:textColor="#FFFFFF"
app:layout_constraintTop_toBottomOf="@+id/progressBar11"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:layout_marginTop="16dp"
android:gravity="center"
/>
<ProgressBar
android:id="@+id/progressBar12"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="0dp"
android:layout_height="10dp"
android:layout_marginStart="45dp"
android:layout_marginTop="1dp"
android:layout_marginEnd="45dp"
android:max="100"
android:progressTint="#FFFFFF"
app:layout_constraintTop_toBottomOf="@+id/timeTextView12"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
<TextView
android:id="@+id/timeTextView13"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="15 Jahre:\nRisiko koronare Herzkrankheit wie Nichtraucher"
android:textSize="16sp"
android:textColor="#FFFFFF"
app:layout_constraintTop_toBottomOf="@+id/progressBar12"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:layout_marginTop="16dp"
android:gravity="center"
/>
<ProgressBar
android:id="@+id/progressBar13"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="0dp"
android:layout_height="10dp"
android:layout_marginStart="45dp"
android:layout_marginTop="1dp"
android:layout_marginEnd="45dp"
android:max="100"
android:progressTint="#FFFFFF"
app:layout_constraintTop_toBottomOf="@+id/timeTextView13"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
<View
android:layout_width="match_parent"
android:layout_height="200dp"
app:layout_constraintTop_toBottomOf="@+id/progressBar13"
android:layout_weight="1" />
</androidx.constraintlayout.widget.ConstraintLayout>
</ScrollView>
<com.google.android.gms.ads.AdView
android:id="@+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|center"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:layout_marginBottom="16dp"
app:adSize="BANNER"
app:adUnitId="ca-app-pub-7889219332409822/8002076735" />
<!-- Der "Zurück"-Button wird außerhalb der ScrollView platziert -->
<Button
android:id="@+id/backButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Zurück"
app:layout_constraintBottom_toTopOf="@+id/adView"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:layout_marginTop="16dp"
android:layout_marginBottom="16dp"/>
</androidx.constraintlayout.widget.ConstraintLayout>
I tried multiple approaches but either the progressbars with smaler maximumamount fill up slower or the progressbars with higher maximum amount fill up slower. I am lost.