1

Centering the toolbar text

I am trying to center my toolbar text but Navigation drawer icon(Hamburger icon) causing my custom toolbar text little right from the center.

Without using Navigation drawer icon toolbar text is perfectly in the center.

activity_main.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:layout_width="match_parent"
    android:id="@+id/drawer_layout"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:openDrawer="start"
    tools:context=".MainActivity">

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

        <android.support.v7.widget.Toolbar 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/toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/colorPrimary"
    android:gravity="center_vertical|center_horizontal|center"
    android:minHeight="?attr/actionBarSize"
    app:contentInsetStart="0dp"
    app:subtitleTextColor="@color/colorWhite"
    app:titleTextColor="@color/colorWhite">

    <android.support.constraint.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:minHeight="?attr/actionBarSize">

        <TextView
            android:id="@+id/toolbar_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="8dp"
            android:layout_marginEnd="8dp"
            android:layout_marginRight="8dp"
            android:layout_marginTop="4dp"
            android:text="Main Title"
            android:textColor="@android:color/white"
            android:textSize="20sp"
            android:textStyle="bold"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_bias="1.0" />

        <TextView
            android:id="@+id/toolbar_subtitle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="8dp"
            android:layout_marginEnd="8dp"
            android:layout_marginRight="8dp"
            android:layout_marginTop="8dp"
            android:text="TextView"
            android:textColor="@android:color/white"
            android:textSize="16sp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/toolbar_title"
            app:layout_constraintVertical_bias="1.0" />

        <!--    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Subtitle"
        android:gravity = "center"
        android:id="@+id/toolbar_Subtitle" />-->

    </android.support.constraint.ConstraintLayout>


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



    <android.support.design.widget.BottomNavigationView
        android:id="@+id/main_bottom_navigation"
        android:layout_width="match_parent"
        android:layout_height="56dp"
        android:layout_alignParentBottom="true"
        android:layout_alignParentStart="true"
        android:background="@color/colorPrimary"
        app:itemIconTint="@drawable/bottom_nav_colors"
        app:itemTextColor="@drawable/bottom_nav_colors"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:menu="@menu/navigation"
        android:layout_alignParentLeft="true" />

    <FrameLayout
        android:id="@+id/home_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_marginBottom="55dp"
        android:layout_marginTop="55dp">

    </FrameLayout>

</RelativeLayout>
    <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"
        android:background="@color/colorWhite"
        app:itemTextColor="@color/colorBlack"

        app:headerLayout="@layout/nav_header_main"
        app:menu="@menu/activity_main_drawer" />

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

In MainActivity.java i am using following code to enable the custom toobar

toolbar=(Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayShowTitleEnabled(false);
Nouman
  • 122
  • 2
  • 11
  • 1
    Change the `ConstraintLayout`'s `layout_width` to `wrap_content`, and add `android:layout_gravity="center"` to it. You might also consider using something other than a `ConstraintLayout` there, since that's a lot of overhead for a simple stack of `TextView`s; e.g., use a vertical `LinearLayout` instead. – Mike M. Jul 18 '18 at 07:38
  • 1
    Change the ConstraintLayout's layout_width to wrap_content, and add android:layout_gravity="center" Thanks @MikeM. this fixed my issue – Nouman Jul 18 '18 at 07:45

0 Answers0