Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

145
Vistas
Switch button animation doesn't work in Drawer Navigation View

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 !!

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

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)
}
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda