Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android changing the width of the navigation drawer panel programmatically

I've used the Navigation Drawer template presents in Android Studio. In the layout of my activity I have this following code:

<fragment android:id="@+id/navigation_drawer"
    android:layout_width="@dimen/navigation_drawer_width"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:name="com.smedilepaolo.newsfeed.NavigationDrawer.NavigationDrawerFragment"
    tools:layout="@layout/fragment_navigation_drawer" />

What I need is to change programmatically the width of the panel. I think that the right zone to change it is this:

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

        mNavigationDrawerFragment = (NavigationDrawerFragment)
                getFragmentManager().findFragmentById(R.id.navigation_drawer);
        mTitle = getTitle();

....

But I can't understand how can achieve my target.

/* UPDATE */

The icon of the drawer panel is not showed in the action bar

    mDrawerListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED);

            mDrawerListView.post(new Runnable() {
                @Override
                public void run() {
                    Resources resources = getResources();
                    float width = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 240, resources.getDisplayMetrics());
                    DrawerLayout.LayoutParams params = (DrawerLayout.LayoutParams) mDrawerListView.getLayoutParams();
                    params.width = (int) (width);
                    mDrawerListView.setLayoutParams(params);

                }
            });

            selectItem(position);
        }
    });



    mDrawerListView.post(new Runnable() {
        @Override
        public void run() {
            Resources resources = getResources();
            float width = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, resources.getDisplayMetrics().widthPixels-1, resources.getDisplayMetrics());
            DrawerLayout.LayoutParams params = (DrawerLayout.LayoutParams) mDrawerListView.getLayoutParams();
            params.width = (int) (width);
            mDrawerListView.setLayoutParams(params);
            mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
            mDrawerLayout.openDrawer(mFragmentContainerView);
        }
    });

That's the selectItem method

    private void selectItem(int position) {
        mCurrentSelectedPosition = position;
        if (mDrawerListView != null) {
            mDrawerListView.setItemChecked(position, true);
        }
        if (mDrawerLayout != null) {
            mDrawerLayout.closeDrawer(mFragmentContainerView);
        }

....
like image 515
Usi Usi Avatar asked Aug 30 '14 23:08

Usi Usi


1 Answers

Based on the code generated from the Navigation Drawer template, you can change the width of the panel by adding this code to the onCreateView() method in the NavigationDrawerFragment.

 mDrawerListView.post(new Runnable() {
        @Override
        public void run() {
            Resources resources = getResources();
            float width = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 100, resources.getDisplayMetrics());
            DrawerLayout.LayoutParams params = (DrawerLayout.LayoutParams) mDrawerListView.getLayoutParams();
            params.width = (int) (width);
            mDrawerListView.setLayoutParams(params);
        }
    });

To disable pan gestures on DrawerLayout:

//NavigationDrawerFragment

public void setUp(int fragmentId, DrawerLayout drawerLayout) {
    mDrawerLayout = drawerLayout;
    mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
}

To open/close DrawerLayout:

mDrawerLayout.openDrawer(mDrawerListView);
mDrawerLayout.closeDrawer(mDrawerListView);

To prevent the icon of the drawer panel disappears, change setOnItemClickListener method as follow:

mDrawerListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            selectItem(position);
        }
});

And modify the overridden onDrawerClose method as follow:

@Override
public void onDrawerClosed(View drawerView) {
    super.onDrawerClosed(drawerView);
    if (!isAdded()) {
        return;
    }

    mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED);

    Resources resources = getResources();
    float width = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 240, resources.getDisplayMetrics());
    DrawerLayout.LayoutParams params = (DrawerLayout.LayoutParams) mDrawerListView.getLayoutParams();
    params.width = (int) (width);
    mDrawerListView.setLayoutParams(params);

    getActivity().invalidateOptionsMenu(); // calls onPrepareOptionsMenu()
}
like image 90
Víctor Albertos Avatar answered Sep 27 '22 17:09

Víctor Albertos