29

I am implementing a navigation drawer for my app. Now it works perfectly except for one small glitch. When I set the Navigation Drawer Icon (ic_drawer) to replace the regular "HomeAsUp" caret icon, I still get the arrow. The Nav Drawer icon does not show. I have implemented every method that was on the android developers website. But it doesn't seem to work.

Below is my code:

DrawerLayout mDrawerLayout;
FrameLayout leftDrawer, rightDrawer, contentFrame;
ActionBarDrawerToggle mDrawerToggle;

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

private void initializeViews() {
    // TODO Auto-generated method stub
    mDrawerLayout = (DrawerLayout) findViewById(R.id.mDrawerLayout);
    mDrawerToggle = new ActionBarDrawerToggle(this,
            mDrawerLayout, R.drawable.ic_drawer,
            R.string.drawer_open_content_desc,
            R.string.drawer_close_content_desc);

    getActionBar().setHomeButtonEnabled(true);
    getActionBar().setDisplayHomeAsUpEnabled(true);

    mDrawerLayout.setDrawerListener(mDrawerToggle);

    leftDrawer = (FrameLayout) findViewById(R.id.drawerLeft_frame);
    rightDrawer = (FrameLayout) findViewById(R.id.drawerRight_frame);
    contentFrame = (FrameLayout) findViewById(R.id.content_frame);
}


@Override
protected void onPostCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onPostCreate(savedInstanceState);
    mDrawerToggle.syncState();
}

@Override
public void onConfigurationChanged(Configuration newConfig) {
    // TODO Auto-generated method stub
    super.onConfigurationChanged(newConfig);
    mDrawerToggle.onConfigurationChanged(newConfig);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.home, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // TODO Auto-generated method stub
    if (mDrawerToggle.onOptionsItemSelected(item)) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}
Adifyr
  • 2,649
  • 7
  • 41
  • 62

5 Answers5

47

I know it is quite late to answer this but this would help someone atleast.

You should probably add these lines of code to show that navigation icon.

@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);
    mDrawerToggle.onConfigurationChanged(newConfig);
}
Naveen Kumar
  • 979
  • 1
  • 13
  • 24
20

Simply put this code in your styles.xml file:

<item name="homeAsUpIndicator">@drawable/ic_drawer</item>
<item name="android:homeAsUpIndicator">@drawable/ic_drawer</item>

I had this same problem and this worked for me.

Programmatically you can set: getSupportActionBar().setHomeAsUpIndicator(R.drawable.ic_drawer);

Dharman
  • 30,962
  • 25
  • 85
  • 135
Filipe Brito
  • 5,329
  • 5
  • 32
  • 42
19

You have to put this code:

getActionBar().setHomeButtonEnabled(true);
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setHomeAsUpIndicator(R.drawable.ic_drawer);

or

getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeAsUpIndicator(R.drawable.ic_drawer);

I hope that this code help you.

Cabezas
  • 9,329
  • 7
  • 67
  • 69
2

I solved the same issue by having a <meta-data> specified for my activity but the android:value points to the same Activity.

Hence, if say Activity name is MainActivity then add the below tag to your activity in the manifest file.

<meta-data
     android:name="android.support.PARENT_ACTIVITY"
     android:value="com.example.MainActivity" />

Hope this helps. :)

Atul O Holic
  • 6,692
  • 4
  • 39
  • 74
  • 2
    Making the `PARENT_ACTIVITY` meta tag point to the same activity in the manifest defeats the entire purpose of setting a parent activity. http://developer.android.com/training/implementing-navigation/ancestral.html – Hassaan May 28 '14 at 14:51
  • Yes, I do know. But somehow only this helped me. Anyways, I feel @Naveen Kumar has also answered correctly below. Please check that before this. – Atul O Holic Jun 26 '15 at 10:29
0

Check in the AndroidManifest.xml, for the that specific activity NOT to have the meta-data "android.support.PARENT_ACTIVITY" set. Try removing the <meta-data> if it's set like this:

<activity> 
...
 <meta-data
    android:name="android.support.PARENT_ACTIVITY"
    android:value="com.company.project.ParentActivity"/>
</activity>
Alex Deac
  • 69
  • 1
  • 3