androidandroid-actionbarclickdrawerandroid-drawer

How to make Drawer icon and logo two different button in ActionBar


I have a problem where I need to be able to click both the logo and the backarrow+hamburger/cater from my action bar. when i don't have anything in my backstack, Hamburger icon is displayed and is opening my left drawer (then via animation it changes to arrow to close the drawer).

Displaying the icon next to AppLogo:

getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);

I have ActionBarDrawerToggle to check if it's left Drawer that is opened (because i have also right drawer that must not interfere with the hamburger icon animations).

ActionBarDrawerToggle

 mDrawerListener = new ActionBarDrawerToggle(this, drawerLayout, R.string.drawer_open, R.string.drawer_closed) {

        //must override these methods to make only left drawer change the icon of drawer in the corner, when opened

        @Override
        public void onDrawerSlide(View drawerView, float slideOffset) {
            if (drawerView.getId() == R.id.left_drawer) {
                super.onDrawerSlide(drawerView, slideOffset);
            }
        }

        @Override
        public void onDrawerOpened(View drawerView) {
            if (drawerView.getId() == R.id.left_drawer) {
                super.onDrawerOpened(drawerView);
            }
        }

        @Override
        public void onDrawerClosed(View drawerView) {
            if (drawerView.getId() == R.id.left_drawer) {
                super.onDrawerClosed(drawerView);
            }
        }
    };
    drawerLayout.setDrawerListener(mDrawerListener);

When navigating bask is possible, the icon changes to the Arrow by disabling the indicator

onBackStackChanged()

@Override
    public void onBackStackChanged() {
        // disable drawer icon and leave backarrow icon if backstack not empty
        boolean canBack = getSupportFragmentManager().getBackStackEntryCount() > 0;
        if (canBack) {
            mDrawerListener.setDrawerIndicatorEnabled(false);
        } else {
            mDrawerListener.setDrawerIndicatorEnabled(true);
        }
    }

In OnOptionsItemSelected() i take care that left drawer (with right one closing) OR a navigateBack action is made.

OnOptionsItemSelected()

public boolean onOptionsItemSelected(MenuItem item) {
    Helper.dismissKeyboard(this);
    switch (item.getItemId()) {
        case android.R.id.home:
            if (getSupportFragmentManager().getBackStackEntryCount() > 0) {
                onSupportNavigateUp();
            } else {
                if (drawerLayout.isDrawerOpen(drawerSports)) {
                    drawerLayout.closeDrawer(drawerSports);
                } else {
                    if (drawerLayout.isDrawerOpen(lvMyAccount)) {
                        drawerLayout.closeDrawer(lvMyAccount);
                    }
                    drawerLayout.openDrawer(drawerSports);
                }

            }
            return true;

Styles.xml

 <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="android:actionBarStyle">@style/MyActionBar</item>
    <item name="actionBarStyle">@style/MyActionBar</item>
</style>

<style name="MyActionBar" >
    <item name="android:background">@color/blue</item>
    <item name="background">@color/blue</item>
    <item name="android:logo">@drawable/logo_marathon</item>
    <item name="logo">@drawable/logo_marathon</item>
    <item name="android:displayOptions">showHome|useLogo</item>
    <item name="displayOptions">showHome|useLogo</item>

</style>
 ...

This is not my project, so i might not know how exactly everything is configured, and i can't make any major changes to it unless necessary, So i need to add onClick method to the logo and I don't know how . I tried <item name="onClick">customLogoClickMethod</item>, but it didn't help.


Solution

  • create the view you want on your actionbar. set all the listeners on it and than set the view to actionbar by getActionBar().setCustomView();

    eg:

    getActionBar().setDisplayOptions(
                    ActionBar.DISPLAY_SHOW_CUSTOM | ActionBar.DISPLAY_USE_LOGO
                            | ActionBar.DISPLAY_SHOW_HOME
                            | ActionBar.DISPLAY_HOME_AS_UP);
    
            LayoutInflater inflater = (LayoutInflater) this
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            // inflate the view that we created before
            View v = inflater.inflate(R.layout.sample_titlebar, null);
    
    //set all the listeners here with that view 
    //and than
    
    getActionBar().setCustomView();