androidnavigation-drawerandroid-toolbardrawerlayoutactionbardrawertoggle

How to make navigation drawer toggle forcibly close all the time


I am using DrawerToggle with toolbar in my project. I have a requirement to make drawer toggle in active (i.e it should not open or close on click or swipe). But I am completely struct how to achieve the things by making the toggle event standstill. I know that my question is quite different but my requirement is to control dynamically on a runtime to control the property of drawertoggle.

I am also posting piece of code I am using for drawertoggle in my project for your reference

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.drawer_layout);

        findViews();


        createFragments ();

        materialColorNames = getResources ().getStringArray(R.array.color_names);

        drawerAdpater = new DrawerAdapter(this,materialColorNames);
        mDrawerList.setAdapter(drawerAdpater);



        if (toolbar != null) {
            changeTitleText ("Droid");

            toolbar.setTitleTextColor (getResources ().getColor (R.color.droid_white));

            setSupportActionBar (toolbar);
        }
        initDrawer();


        updateFragment (0);
        mDrawerList.setOnItemClickListener(new DrawerItemClickListener());
    }


    @Override
    protected void onResume () {
        super.onResume ();



    }

    private void changeTitleText (CharSequence title) {
        toolbar.setTitle(title);
    }

    private void createFragments () {

        myProjectsFragment = new MyProjectsFragment();

    }

    private void findViews() {
        mDrawerList = (ListView) findViewById(R.id.left_drawer);
        toolbar = (Toolbar) findViewById(R.id.toolbar);

        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);


    }

    private void initDrawer() {

        drawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, toolbar, R.string.drawer_open, R.string.drawer_close) {

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

            }

            @Override
            public void onDrawerOpened(View drawerView) {
                super.onDrawerOpened(drawerView);
//                mDrawerLayout.closeDrawer(mDrawerList);



            }
        };
        mDrawerLayout.setDrawerListener(drawerToggle);
    }

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

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



private void updateFragment(int position) {

        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();

        switch (position) {

            case 0:
                ft.replace(R.id.content_frame, myProjectsFragment);
                break;
}

Kindly please help me with this solution. I am seraching this to be done for the long time. Thanks in advance. Please let me know through comments if my question is not clear.


Solution

  • In your onCreate() after drawer layout and list are found call:

    mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED, mDrawerList);
    drawerToggle.setDrawerIndicatorEnabled(false);
    

    From the Javadoc:

    public void setDrawerLockMode (int lockMode, View drawerView)
    

    Enable or disable interaction with the given drawer.

    This allows the application to restrict the user's ability to open or close the given drawer. DrawerLayout will still respond to calls to openDrawer(int), closeDrawer(int) and friends if a drawer is locked.