I am quite new to Android Studio and Kotlin programming. I'm facing an issue regarding the animation of the left button of the Toolbar in my app.
I have an Activity in which I load different Fragments for different purposes. These Fragments can be opened using the Drawer Layout. Everytime I switch between two Fragments, the ActionBarDrawerToggle Indicator (the so-called "Hamburger icon") remains as it is, that is it remains the Hamburger icon. This is ok to me, I want this behaviour
Anyway, I have this Fragment (say A) where there is a circular ImageView with a click listener that, when clicked, opens - with a smooth and completely working animation - a new Fragment (say B) in which the Image is shown in full size. Here comes my problem. I want that in Fragment B the Hamburger icon slowly transform into the supportActionBar Up Button (or back button), which will allow me to click it and go back to A.
In this moment I have successfully set the change of the two icons when the Fragments change, and I can see Hambrurger in A and the back arrow in B, but I can't find a solution to have the animation
Thanks to all in advance!
Below is my code:
MainActivity
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
//set toolbar
setContentView(R.layout.drawer_home)
toolbar = findViewById(R.id.drawer_toolbar)
setSupportActionBar(toolbar)
//set drawer layout
drawerLayout = findViewById(R.id.drawer)
navigationView = findViewById(R.id.navigationView)
navigationView.setNavigationItemSelectedListener(this)
actionBarDrawerToggle = ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.close, R.string.open)
drawerLayout.addDrawerListener(actionBarDrawerToggle)
actionBarDrawerToggle.isDrawerIndicatorEnabled
actionBarDrawerToggle.syncState()
}
//I call this function inside the Fragments A and B where I want to set the Hamburger Icon and the Back Button when needed
fun showBackButton (enable: Boolean){
if(enable){ //show back button
//block drawer
drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED)
//remove hamburger icon
actionBarDrawerToggle.isDrawerIndicatorEnabled = false
//show back button
supportActionBar?.setDisplayHomeAsUpEnabled(true)
//set listener on up button
if(!toolBarNavigationListenerIsRegistered){
actionBarDrawerToggle.setToolbarNavigationClickListener {
//il back button รจ stato premuto, ritornare indietro nella backstack
val fragmentManager = supportFragmentManager
fragmentManager.popBackStack()
}
toolBarNavigationListenerIsRegistered = true
}
}else{ //show hamburger icon
//unlock drawer
drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED)
//disable back button
supportActionBar?.setDisplayHomeAsUpEnabled(false)
//enable hamburger icon
actionBarDrawerToggle.isDrawerIndicatorEnabled = true
//cancel listener on up button
actionBarDrawerToggle.toolbarNavigationClickListener = null
toolBarNavigationListenerIsRegistered = false
}
}