I want my TextView for textViewSteps
to go back to 0 at the end of each day.
The TextView counts the user's steps and at 12am each day it should reset to 0.
I'm not too worried about saving the steps from that day, just need to reset it for now.
This is the XML for the steps in activity_main.xml
<RelativeLayout
android:id="@ id/stepsContainer"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_marginTop="20dp"
android:layout_marginBottom="20dp"
android:orientation="horizontal"
android:weightSum="2"
>
<ImageView
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:background="@drawable/rounded_rectangle"
android:orientation="horizontal"
/>
<TextView
android:layout_marginTop="50dp"
android:id="@ id/textViewSteps"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:text="0"
android:textColor="@color/black"
android:textSize="30sp"
android:textStyle="bold" />
<TextView
android:id="@ id/stepsDailyGoal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@ id/textViewSteps"
android:layout_centerHorizontal="true"
android:text="/10,000"
android:textColor="@color/black"
android:textSize="30sp"
android:textStyle="bold" />
</RelativeLayout>
I have a function in my MainActivity.kt
for counting the steps
override fun onSensorChanged(event: SensorEvent?) {
if (running){
totalSteps = event!!.values[0]
val stepCount = totalSteps.toInt() - previousTotalSteps.toInt()
binding.textViewSteps.text = ("$stepCount")
}
}
and another function to reset the steps, which is currently empty as I don't know what to do.
fun resetSteps(){}
I tried to look around on other forums but they all seem to be in Java and not Kotlin or they aren't looking to reset it at 12am each day.
I also looked through the Android docs and the Kotlin docs but found nothing of use.
CodePudding user response:
To keep a value across app restarts and system reboots, you need to persist it to the device, usually in a text file or database. For the automatic daily reset, you could possibly use something like Android WorkManager and have it schedule a task to run at 12am and reset the persistent value.
CodePudding user response:
You need to reset the steps count when day changed you can do this simple way.
Save current day in SharedPreferences
and the next day check condition SharedPreferences
day not matched with current day then reset the Steps counter.