Replacing the burger icon with an arrow

It turned out to synchronize the state of the side menu with the burger button via ActionBarDrawerToggle. I.e., the burger icon turns into an arrow when the menu appears. Now I need to make it so that when you click on the search button in the toolbar, the state of ActionBarDrawerToggle changes from the burger to the arrow and back. Closed search

Opened search incorrectly

Closed search correctly

 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
mAppBarConfiguration = new AppBarConfiguration.Builder(
            R.id.nav_home, R.id.nav_gallery, R.id.nav_slideshow,
            R.id.nav_tools, R.id.nav_share, R.id.nav_send)
            .setDrawerLayout(drawer)
            .build();

    toggle = new ActionBarDrawerToggle(
            this,
            drawer,
            R.string.navigation_drawer_open,
            R.string.navigation_drawer_close);
    toggle.setDrawerIndicatorEnabled(true);
    //noinspection deprecation
    drawer.setDrawerListener(toggle);

    NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
    NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
    NavigationUI.setupWithNavController(navigationView, navController);}

public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.


    getMenuInflater().inflate(R.menu.main, menu);





    SearchManager searchManager =
            (SearchManager) getSystemService(Context.SEARCH_SERVICE);
    SearchView searchView =
            (SearchView) menu.findItem(R.id.app_bar_search).getActionView();
    searchView.setSearchableInfo(
            searchManager.getSearchableInfo(getComponentName()));
    searchView.setMaxWidth(Integer.MAX_VALUE);


    return true;
}
@Override
public boolean onSupportNavigateUp() {
    NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
    return NavigationUI.navigateUp(navController, mAppBarConfiguration)
            || super.onSupportNavigateUp();
}
Author: Mr.Krotos, 2020-02-05

1 answers

In a good way, this burger was created specifically in a pair of DrawerLayout, and tying something else to it is not a good thing. It would be necessary to write your own to avoid any strange conflicts.

In general, you can set any state for it using the toggle.onDrawerSlide(View, Float) method. I don't remember what kind of View is passed there, the Float value is taken from 0 to 1, where 0 is a burger, 1 is an arrow, 0.5 is halfway from one to the other.

You can create a VelueAnimator from run change from 0 to 1, if you dig into the source code, you will find the used interpolator and the duration for the NavView "exit" to use them on the animation when activating the search, otherwise you will have one animation speed when you click on the burger, and another when you click on the search.

You still need to decide what to do if the user pulled out the NavView gesture when the search is highlighted and toggle is already in the form of an arrow. If ignored , there will be a jump from the arrow to the burger and the animation will start back to the arrow is proportional to the NavView extension

 1
Author: Спицко Дмитрий, 2020-02-06 14:04:01