kiana rahimi
kiana rahimi

Reputation: 246

Hamburger icon doesn't show as default icon in toolbar

This is my setUpToolbar method . and it's called in onCreate in mainActivity . my question is , why humberger icon doesn't show in toolbar and toolbar show the back navigation icon in toolbar .

this is my setUptoolbar method.

private void setUpToolbar() {

        Toolbar archiveToolbar = (Toolbar)findViewById(R.id.xmlToolbarMain);
        setSupportActionBar(archiveToolbar);

        getSupportActionBar().setDisplayShowTitleEnabled(false);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    }

and This is my toolbar xml :

 <android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/xmlToolbarMain"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="#34465d"
            android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
            android:transitionName="actionBar"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
            />

and this is my style under the values dir :

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>


</resources>

enter image description here

Upvotes: 1

Views: 4979

Answers (4)

Dhaval Parmar
Dhaval Parmar

Reputation: 18978

you have to add that icon by adding below code in setUpToolbar method

getSupportActionBar().setHomeAsUpIndicator(R.drawable.ic_menu);// whatever your icon name
getSupportActionBar().setDisplayHomeAsUpEnabled(true);

Upvotes: 1

N J
N J

Reputation: 27515

First Approach

manually set custom hamburger icon

getSupportActionBar().setIcon(R.drawable.your_hamburger_icon);

or

Hamburger is for ActionBarDrawerToggle

Then add DrawerLayout and ActionBarDrawerToggle

Upvotes: 0

Sergey Trukhachev
Sergey Trukhachev

Reputation: 538

For this feature you need to add ActionBarDrawerToggle object and sync it with the toolbar.

Something like this:

public class DrawerActivity extends AppCompatActivity {

private DrawerLayout drawerLayout;
private ActionBarDrawerToggle drawerToggle;

...

private void setupDrawerLayout() {
     drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
     drawerToggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.open, R.string.close);
     drawerLayout.setDrawerListener(drawerToggle);
}

...

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

This should help.

Upvotes: 4

Stanojkovic
Stanojkovic

Reputation: 1632

Add this to your code:

toolbar.setNavigationIcon(R.drawable.hamburger);
toolbar.setTitle("");

Upvotes: 1

Related Questions