1

I implemented a drawer navigation using fragments. After implementing it following the google tutorials, the hamburger icon appears on the left as it should. However when I make a change to my toolbar xml to add textview at the center, it gets moved to the left, I assume by the drawer toggle but I don't see it anywhere in my xml or main.java. This is what I have for my mainActivity class onCreate method where the nav bar is set up i think (again i used the drawer activity class):

public class MainActivity extends AppCompatActivity
    implements NavigationView.OnNavigationItemSelectedListener {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
            this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
    drawer.setDrawerListener(toggle);
    toggle.syncState();
    NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this);
}

And this is my XML for the app bar, app_bar_main.xml

 <?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.closedbracket.drawernav.MainActivity">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay">

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center">

                <TextView
                    android:id="@+id/title"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="title "
                    android:textAllCaps="false"
                    android:textColor="@android:color/background_light"
                    android:textSize="22sp"
                    android:layout_alignParentTop="true"
                    android:layout_centerHorizontal="true" />
            </RelativeLayout>

        </android.support.v7.widget.Toolbar>

    </android.support.design.widget.AppBarLayout>

    <include layout="@layout/content_main" />

</android.support.design.widget.CoordinatorLayout>

I've also attached some images to show what I mean happens. The first image is the layout of the app_bar_main.xml and the second image is how it looks when the app runs and the drawer hamburger icon is set. app_bar_main.xml layout. The app during runtime, with the title now shifted left.

What I really need help with is: 1) How to center the title whilst taking into account the space the toggle bar takes up (not just a few pixels to the left since it might change with screen size?)

2)How do I find the toggle button, and where its placed in the action bar and modify it? If I wanted to move it to the right instead or somewhere else is that possible?

Thank you so much for any help, or suggestions!

ZarifS
  • 467
  • 1
  • 11
  • 20
  • refer to this answer https://stackoverflow.com/a/28177672/5353830 – Rai_Gaurav Jul 14 '17 at 05:50
  • You need to understand that Toolbar widget extends ViewGroup class, and it has it's own LayoutParams. So you do not require RelativeLayout inside Toolbar, and need to add just single line with TextView. – Rai_Gaurav Jul 14 '17 at 05:55
  • refer to this https://stackoverflow.com/a/33729909/5353830 , this will solve your first problem – Rai_Gaurav Jul 14 '17 at 06:08

0 Answers0