Search code examples
androidandroid-actionbartoolbarandroid-toolbar

Aligning the ActionBar icon


I followed this tutorial to create a Navigation Drawer activity. I have the following code:

Activity XML

<?xml version="1.0" encoding="utf-8"?>

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context=".Activities.DashboardActivity">

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

<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:background="@color/login_blue"
    android:theme="@style/MyNavbarTheme" />

</FrameLayout>

<android.support.design.widget.NavigationView
    android:id="@+id/nav_view"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:fitsSystemWindows="true"
    app:menu="@menu/drawer_view"
    app:headerLayout="@layout/nav_header"/>

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

Activity code

....

ActionBar actionbar = getSupportActionBar();

try {
    actionbar.setDisplayHomeAsUpEnabled(true);
    actionbar.setHomeAsUpIndicator(R.drawable.icon_menu_white);
}
catch (NullPointerException err) {
    err.printStackTrace();
}

...

However, the menu icon isn't aligned with the text (see image). The icon is slightly above the center. How can I fix this? Also how can I change the color of the title text? enter image description here


Solution

  • This is how I managed to solve this issue.

    I removed this from my code

    actionbar.setDisplayHomeAsUpEnabled(true);
    actionbar.setHomeAsUpIndicator(R.drawable.icon_menu_white);
    

    and used this instead:

     ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
         this, mDrawerLayout, toolbar, R.string.text_in, 
         R.string.text_out);
     mDrawerLayout.addDrawerListener(toggle);
     toggle.syncState();