16

I am trying to add the navigation drawer in my app.. Everything is working fine But now I still got the arrow icon although I replaced it with the ic_drawer from Android? Here's my code:

private ActionBarDrawerToggle mDrawerToggle;


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

    DrawerLayout mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
    ExpandableListView mDrawerList = (ExpandableListView) findViewById(R.id.left_drawer);

    mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow,
            GravityCompat.START);
    mDrawerList.setAdapter(new DrawerListAdapter(this));
    mDrawerList.setOnChildClickListener(new DrawerItemClickListener());

    mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
            R.drawable.ic_launcher, R.string.drawer_open,
            R.string.drawer_close);

    Log.d("Mudit",
            "mDrawerToggle" + mDrawerToggle.isDrawerIndicatorEnabled()); // returns true

    mDrawerLayout.setDrawerListener(mDrawerToggle);

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

    getActionBar().setDisplayShowHomeEnabled(false);

}

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

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

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

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    if (mDrawerToggle.onOptionsItemSelected(item)) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

XML:

 <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" >

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

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" >

        </LinearLayout>
    </ScrollView>

    <ExpandableListView
        android:id="@+id/left_drawer"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:background="#ffffff"
        android:dividerHeight="0dp" />

</android.support.v4.widget.DrawerLayout>

I have looking at various code samples online but all of them are doing same thing. I dont know why it is not working for me.

Please help.

mudit
  • 25,306
  • 32
  • 90
  • 132

4 Answers4

22

You'll have to use

getActionBar().setDisplayShowHomeEnabled(true);

instead of

getActionBar().setDisplayShowHomeEnabled(false);

Cheers,

Felix

EDIT: Here's how to hide the Icon via XML:

add this to your styles.xml: (Replace android:Widget.Holo.Light.ActionBar.Solid with android:Widget.Holo.ActionBar.Solid if you are using the dark action bar)

<style name="ActionBarStyle" parent="android:Widget.Holo.Light.ActionBar.Solid">
    <item name="android:icon">@android:color/transparent</item>
</style>

and then add this to your Theme you have defined in AndroidManifest.xml (Standard is the AppBaseTheme in /res/values-v11/styles.xml - AppBaseTheme:

<item name="android:actionBarStyle">@style/ActionBarStyle</item>

Hope this helps!

fekle
  • 246
  • 3
  • 7
  • Thanks for the reply.. but i do not want to show the app icon in title bar. is there a way i can do so? – mudit Jun 28 '13 at 08:50
  • That's the EXACT thing I wanted to do myself :-) You can't do this Programmatically, you'll have to do it in your XML - check my edited Answer ;) – fekle Jun 28 '13 at 09:02
  • Implemented the same thing. Thanks for the help!! – mudit Jun 28 '13 at 09:14
22

try this code

   @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        // Sync the toggle state after onRestoreInstanceState has occurred.
        actbardrawertoggle.syncState();
    }
Sunil Kumar
  • 7,086
  • 4
  • 32
  • 50
  • 6
    This is what worked for me, I had an "<" instead of the "hamburger" (the ic_drawer) icon. After overriding onPostCreated it shows the ic_drawer instead of the "<". – dazito Apr 29 '14 at 12:15
2

This worked for me when I wanted to get the menu icon back from the back arrow

private void updateNavigationIcon() {
    if (getSupportFragmentManager().getBackStackEntryCount() == 0
            && getSupportActionBar() != null) {
        getSupportActionBar().setDisplayHomeAsUpEnabled(false);
        toggle.syncState();
    }
}
XurajB
  • 820
  • 1
  • 14
  • 29
1

Just add the following line inside showGlobalContextActionBar() method (NavigationDrawerFragment.java class)

actionBar.setHomeAsUpIndicator(R.drawable.ic_action_menu);

Code snippet:

private void showGlobalContextActionBar() {
        ActionBar actionBar = getActionBar();
        actionBar.setDisplayShowTitleEnabled(true);
        actionBar.setHomeAsUpIndicator(R.drawable.ic_action_menu);
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
        actionBar.setTitle(R.string.app_name);
    }
Maddy
  • 316
  • 1
  • 6
  • 14