rrrocky
rrrocky

Reputation: 696

Navigation Drawer is not opening when sliding from left

As the page on Android Developers says

The user can bring the navigation drawer onto the screen by swiping from the left edge of the screen or by touching the application icon on the action bar.

But strangely the navigation drawer on my activity does not respond to sliding action. It toggles only on touching the icon on the action bar. Below is my implementation of the navigation drawer

 mNavigationDrawerFragment = (NavigationDrawerFragment)
            getSupportFragmentManager().findFragmentById(R.id.navigation_drawer);

    // Set up the drawer.
    drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
    mNavigationDrawerFragment.setUp(
            R.id.navigation_drawer,
            drawerLayout);

Is there any possible explanation for this? What I doubt is my activity by default has the layout of one of its fragments. So is that the reason?

Edit: The layout file of my activity

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainPage">

<!-- The main ocntent view -->
<FrameLayout android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
</FrameLayout>


<!-- The navigation drawer-->
<fragment android:id="@+id/navigation_drawer"
    android:layout_width="240dp"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:divider="@android:color/transparent"
    android:name="com.example.android.handsfree.NavigationDrawerFragment"
    tools:layout="@layout/fragment_navigation_drawer" />

Upvotes: 6

Views: 4098

Answers (5)

Xyaren
Xyaren

Reputation: 953

Your ´NavigationDrawerFragment´ already creates a drawer Toggle for you inside its `setUp´ method.

You should not create a new one inside your ´MainPage´ Activity.


Notes: You can use the android.support.v7.app.ActionBarDrawerToggle instead of the v4 one inside the NavigationDrawerFragment.


Update:

The problem seems solved now. There were 2 Issues:

  1. The OP created a second drawer toggle in it's MainPage - Activity but there was already one created inside the NavigationDrawerFragment's setUp method, which gets called by the MainPage in order to set up the drawer. (This is basicly outsourc[ecoding]ing some of the drawer stuff to the drawer fragment.)
  2. The OP locked the drawer inside onCreateOptionsMenu by calling a method which sets the DrawerLockMode to LOCK_MODE_LOCKED_CLOSED. He never reverted this change.

Upvotes: 1

Ali Noureddine
Ali Noureddine

Reputation: 323

1- replace the tag by :

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainPage">

    <!-- The main ocntent view -->
    <FrameLayout android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    </FrameLayout>


    <!-- The navigation drawer-->
    <ListView android:id="@+id/navigation_drawer"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:divider="@android:color/transparent" />
    </android.support.v4.widget.DrawerLayout>

2- use this code in the onCreate function u can use the v4 ActionBarDrawerToggle or the v7 her i use the v4:

private DrawerLayout mDrawerLayout;
private ActionBarDrawerToggle mDrawerToggle;

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

mDrawerToggle = new ActionBarDrawerToggle(this,    mDrawerLayout,R.drawable.ic_drawer,
R.string.app_name,R.string.app_name) {
                    public void onDrawerClosed(View view) {
                        getActionBar().setTitle(mTitle);
                        invalidateOptionsMenu();
                    }

                    public void onDrawerOpened(View drawerView) {
                        getActionBar().setTitle(mDrawerTitle);
                        invalidateOptionsMenu();
                    }
                };
                mDrawerLayout.setDrawerListener(mDrawerToggle);

Upvotes: 0

Anggrayudi H
Anggrayudi H

Reputation: 15155

Add these fields in your activity class:

private ActionBarDrawerToggle mDrawerToggle;
private DrawerLayout mDrawerLayout;

Then, inside your activity:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ...
    // enabling action bar app icon and behaving it as toggle button
    getActionBar().setDisplayHomeAsUpEnabled(true);
    getActionBar().setHomeButtonEnabled(true)

    mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
    mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
                R.drawable.ic_drawer, //nav menu toggle icon
                R.string.app_name, // nav drawer open - description for accessibility
                R.string.app_name // nav drawer close - description for accessibility
        ){
            public void onDrawerClosed(View view) {
                // calling onPrepareOptionsMenu() to show action bar icons
                invalidateOptionsMenu();
            }

            public void onDrawerOpened(View drawerView) {
                // calling onPrepareOptionsMenu() to hide action bar icons
                invalidateOptionsMenu();
            }
        };
        mDrawerLayout.setDrawerListener(mDrawerToggle);

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // toggle nav drawer on selecting action bar app icon/title
    if (mDrawerToggle.onOptionsItemSelected(item)) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

/**
 * When using the ActionBarDrawerToggle, you must call it during
 * onPostCreate() and onConfigurationChanged()...
 */

@Override
protected void onPostCreate(Bundle savedInstanceState) {
    super.onPostCreate(savedInstanceState);
    // Sync the toggle state after onRestoreInstanceState has occurred.
    mDrawerToggle.syncState();
}

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    // Pass any configuration change to the drawer toggls
    mDrawerToggle.onConfigurationChanged(newConfig);
}

ic_drawer is a .png image which located in drawable resources. You can search this image on Google.

API level to be used is 11+.

Hope it helps for you.

Upvotes: 0

Akash
Akash

Reputation: 971

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white" >

<FrameLayout
    android:id="@+id/content_frame"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

<ListView
    android:id="@+id/listview_drawer"
    android:layout_width="240dp"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:background="@color/drawer"
    android:choiceMode="singleChoice" />

on your activity

drawlayout = (DrawerLayout) findViewById(R.id.drawer_layout);
    listData = (ListView) findViewById(R.id.listview_drawer);

    drawlayout.setDrawerShadow(R.drawable.drawer_shadow,
            GravityCompat.START);

Upvotes: 0

amar
amar

Reputation: 147

You should use:

drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED);

Upvotes: 0

Related Questions