1

After spending a lot of time and not finding a suitable solution, I decided to turn to this service for help.
I have an example of the simplest application that implements Drawer. This example contains only menu items without any implementations. In the menu item Share, I added a Switch and a listener to it:

main_nav.xml

<item
    android:id="@+id/nav_share"
    android:icon="@drawable/ic_menu_share"
    app:actionViewClass="androidx.appcompat.widget.SwitchCompat"
    android:title="@string/menu_share" />

MainActivity.kt

  val menuItem = navigation_view.menu.findItem(R.id.nav_share)
  val switch_id = menuItem.actionView as SwitchCompat
  switch_id.isChecked = true
  switch_id.setOnClickListener {
   // TODO: empty scope
  }

The switch works smoothly and with animation as shown below:

enter image description here

But now I need to turn off the Home menu item if the Switch is off:

switch_id.setOnClickListener {
     navigation_view.menu.findItem(R.id.nav_home).isEnabled = switch_id.isChecked
}

After adding this line, my Switch animation breaks!

enter image description here

I spent two days solving this issue and in the end to no avail.

My question: WTF with android menu in 2021? Why does accessing a menu item break the animation and how can I fix it? I would be grateful for any help !!

Tomas
  • 1,567
  • 3
  • 21
  • 38
  • Did you try using `setOnCheckedChangeListener` and using the provided boolean instead of `setOnClickListener` ? Example: ```switch_id.setOnCheckedChangedListener { _, isChecked -> navigation_view.menu.findItem(R.id.nav_home).isEnabled = isChecked } ``` – Kilian Sep 11 '21 at 17:12
  • @Kilian thanks for your answer, but unfortunately this method doesn't work. – Tomas Sep 11 '21 at 23:27
  • @Kilian Perhaps it has something to do with these answer https://stackoverflow.com/a/57714721/4512868 and the issue https://issuetracker.google.com/issues/37074309, but I'm not sure – Tomas Sep 11 '21 at 23:31

1 Answers1

0

I created an issue with this question in Android Tracker and got an answer:

Either way, this is likely to be a non-trivial fix for an issue that is not a regression and not impacting correctness of the UI. At this time, we have no plans to address the issue.

And then a workaround was suggested:

Something along the lines of adding a setOnCheckedChangeListener that posts a delayed Runnable to effect the change. I'd recommend asking on StackOverflow if you need a detailed example.

E.g. How to delay "runOnUiThread" in android?

I added a postDelay(200) and the Switch animation works fine:

switch.setOnCheckedChangeListener { _, isChecked ->
     Handler(Looper.getMainLooper()).postDelayed({
         navigationView.menu.findItem(R.id.nav_home).isEnabled = isChecked
     }, 200)
}
Tomas
  • 1,567
  • 3
  • 21
  • 38