5

I'm using a toolbar with a DrawerLayout. My toolbar has 2 views (buttons), one in the center and one close to the right margin. When I turn on the burger icon with getSupportActionBar().setDisplayHomeAsUpEnabled(true), my central button gets moved slightly to the right.
I managed to center the button by calculating the left margin based on the toolbar width, subtracting the burger icon size and 1/2 of my icon size. I'm looking for a better solution as this might not work well with all densities and screen sizes.

centered, no burger off center

Here's my main layout

<android.support.v4.widget.DrawerLayout
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:fitsSystemWindows="false"
    android:layout_height="match_parent">
    <!-- The main content view -->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
        <include layout="@layout/ati_toolbar"/>

        <FrameLayout android:id="@+id/main_container"
            android:layout_width="match_parent"
            android:layout_weight="1"
            android:layout_height="match_parent" />

    </LinearLayout>
    <!-- The navigation drawer -->
    <ListView android:id="@+id/left_drawer"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:choiceMode="singleChoice"
        android:divider="@android:color/transparent"
        android:dividerHeight="0dp"
        android:background="#111"/>
</android.support.v4.widget.DrawerLayout>

and this is the ati_toolbar layout

<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/atiToolBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/Toolbar_Theme">
<RelativeLayout
    android:id="@+id/menuBar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:baselineAligned="false">
        <ImageButton
            android:id="@+id/btnask"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:src="@drawable/ask_button"
            android:background="@color/ATIBlack"
            />

    <EditText
        android:id="@+id/searchBox"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:text="SEARCH"
        android:layout_marginRight="20dp"
        android:textSize="15dp"
        android:background="@color/background_material_light" />
</RelativeLayout>

Toolbar and Drawer setup in main activity

    toolbar = (Toolbar) findViewById(R.id.atiToolBar);
    if (toolbar != null) {
        setSupportActionBar(toolbar);   

    }
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);  



    mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);  
    mDrawerList = (ListView) findViewById(R.id.left_drawer);  
    List<Category> cList = null;
    _cAdapter = new CategoryListAdapter(cList,getBaseContext());
    mDrawerList.setAdapter(_cAdapter);
    CategoryService cs = new CategoryService(getBaseContext(), _cAdapter);
    cs.load();

    mDrawerList.setOnItemClickListener(new DrawerItemClickListener()); // Listens to clicks on categories

    // Drawer Toggle management
    mDrawerToggle = new ActionBarDrawerToggle(
            this,                  /* host Activity */
            mDrawerLayout,         /* DrawerLayout object */
             //  toolbar,
            R.string.drawer_open,  /* "open drawer" description */
            R.string.drawer_close  /* "close drawer" description */
    );
    mDrawerLayout.setDrawerListener(mDrawerToggle);
LeandroG
  • 831
  • 9
  • 17
  • Pretty sure you can set the title margin left to -56dp or -72dp. But it may not be exact so you would need to find out the specs of the width of the hamburger. – Eugene H Aug 04 '15 at 14:47
  • You figure this issue out? I am having the issue when two icons appear on the toolbar... – Lion789 Mar 22 '16 at 01:00
  • Any solution you found?? I am facing same issues.. It will be great if you can help. – Subhalaxmi May 17 '16 at 19:07
  • Sorry I couldn't get it to work the way I liked it.. I ended up using an icon with the burger image and triggered the drawer from there – LeandroG May 29 '16 at 22:42
  • check this http://stackoverflow.com/questions/42530187/toolbar-isnt-in-the-center-of-the-appbarlayout/42530287#42530287 – Arpan Sharma Mar 07 '17 at 11:06

1 Answers1

1

Use app:contentInsetStartWithNavigation="0dp" on your toolbar. Hope this helps.

Thewads
  • 4,953
  • 11
  • 55
  • 71
Saurabh
  • 139
  • 1
  • 8