'Android Progress bar inside menu item is not updating properly
I have two sensor connected with my android app. Both apps are connecting over BLE and I get two types of data from these sensors.
- Sensor Data
- Power Value
To show the power status, I followed a tutorial to create the progressbar inside actionbar menu as follows.
theme.xml
<!-- Power Progress Theme -->
<style name="PowerProgressBar">
<item name="android:indeterminateOnly">false</item>
<item name="android:progressDrawable">@drawable/ic_action_power_progress</item>
</style>
layout_power_progress.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
style="@android:style/Widget.ActionButton">
<ProgressBar
android:id="@+id/powerProgress"
style="@style/PowerProgressBar"
android:layout_width="24dp"
android:layout_height="24dp"
android:visibility="visible"
android:layout_gravity="center" />
</FrameLayout>
menu/main.xml
<item
android:id="@+id/action_power_sensor_1"
android:title="@string/action_power_sensor_1"
app:actionLayout="@layout/layout_power_progress"
app:showAsAction="always" />
<item
android:id="@+id/action_power_sensor_2"
android:title="@string/action_power_sensor_2"
app:actionLayout="@layout/layout_power_progress"
app:showAsAction="always" />
override fun onPrepareOptionsMenu(menu: Menu?): Boolean {
/*---------- Sensor 1 ---------------*/
sensor1PowerProgressView = menu?.findItem(R.id.action_power_sensor_1)?.actionView?.findViewById(R.id.powerProgress)
sensor1PowerProgressView ?.max = 5
sensor1PowerProgressView ?.progress = sensor1Power
/*---------- Sensor 2 ---------------*/
sensor2PowerProgressView = menu?.findItem(R.id.action_power_sensor_2)?.actionView?.findViewById(R.id.powerProgress)
sensor2PowerProgressView ?.max = 100
sensor2PowerProgressView ?.progress = sensor2Power
return super.onPrepareOptionsMenu(menu)
}
/*--- Code where the power is getting updated ----*/
sensorViewModel.getSensor1Power().observe(this) {
sensor1Power = it
if (sensor1PowerPrev!=sensor1Power) {
sensor1PowerPrev = sensor1Power
invalidateOptionsMenu()
}
}
sensorViewMode2.getSensor2Power().observe(this) {
sensor1Power = it
if (sensor2PowerPrev!=sensor2Power) {
sensor2PowerPrev = sensor2Power
invalidateOptionsMenu()
}
}
I am updating these power values from sensorViewModel. I see the values inside the sensorViewModel getting updated properly upon sensor connect and disconnect. I am logging both values (sensor1Power & sensor2Power ) inside the onPrepareOptionsMenu and I see the values getting logged there properly.
Now after spending whole 2 weeks on this issue I can't figure out why sensor1PowerProgressView and sensor2PowerProgressView are not getting updated on disconnect. They retain the last value.
Anyone, can point out what am I doing incorrectly. if you need any more details, I can share upon request.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
