Tuesday 6 September 2016

ROWING without OARS : Vexed case of NavigationDrawer with AppCompatActivity and Causal Flaws.



This post is about resolving few issues with the implementation of NavigationDrawer with AppCompatActivity. 


Really?
Na.
Well its more than that.

As there are always two ways for everything, so does exists two ways to implement a NavigationDrawer.

a). Conventional way of creating navigation drawer using a ListView and a FrameLayout hosting fragments.
b). Designer's way to create navigation drawer using  NavigationView and a headerlayout to show a profile picture. 

This issue started when to speed up some task, I preferred using conventional Navigation Drawer instead of new NavigationView based drawer from Design Support Library.


1. Started working with NaviagtionDrawer with an activtiy extending from AppCompatActivity.
   Everything will be good when you will create a layout with DrawerLayout as its root element and inside it residing a FrameLayout and a ListView.
   You will set the contentView to the activity and refer each view, set listeners to each etc.
 
   Issue : ListView fills screen irrespective of whatever layout_width and layout_height you set and lie over the content frame layout.
   Cause & Resolution : never forget this attribute android:layout_gravity="start" 

   It was a mistake because it wasn't an act of Copy-Paste.


2. Next one is also very silly as you must update/upgrade with time. If you don't, get ready for consequences. How to show the hamburger icon ?
 
   Issue : Hamburger icon not showing
   Cause & Resolution : When use ActionBarDrawerToggle, use the class from  android.support.v7.app.ActionBarDrawerToggle instead of android.support.v4.app.ActionBarDrawerToggle;


        mActionBarDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.string.open_drawer
                , R.string.close_drawer) {
            @Override
            public void onDrawerOpened(View drawerView) {
                super.onDrawerOpened(drawerView);
                supportInvalidateOptionsMenu();
            }

            @Override
            public void onDrawerClosed(View drawerView) {
                super.onDrawerClosed(drawerView);
                supportInvalidateOptionsMenu();
            }
        };
 

    //  Also update your Actionbar accordingly
       ActionBar actionBar = getSupportActionBar();

        actionBar.setDisplayShowHomeEnabled(true);
        actionBar.setDisplayHomeAsUpEnabled(true);
        actionBar.setDisplayShowTitleEnabled(true);

3. Last but not the least how to enable the toggling of hamburger icon. 

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Pass the event to ActionBarDrawerToggle, if it returns true, then it has handled the app icon touch event
        
if (mActionBarDrawerToggle.onOptionsItemSelected(item)) {
            return true;
        }
        
        return super.onOptionsItemSelected(item);
    }


Few more points to take care if Navigation Drawer is not showing and and no action on click of hamburger icon.

    @Override
    protected void onPostCreate(@Nullable Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        mActionBarDrawerToggle.syncState();
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        mActionBarDrawerToggle.onConfigurationChanged(newConfig);
    }

 


PS : It's not that following this post and learning from my mistakes may end all the problems related to Navigation Drawer. 
You might create greater problems than I what I did. But you can work out the above given issues.