0

Below is my custom toolbar xml. I'm trying hard to make toolbar title at center but fails all the time.

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/tool_bar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:minHeight="?attr/actionBarSize"
    android:background="@color/white">

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

        <TextView
            android:id="@+id/toolbar_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_toLeftOf="@+id/rlLogout"
            android:text="Long text title"
            android:textColor="@color/title_color"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:fontFamily="sans-serif-light"
            android:layout_centerVertical="true"
            android:layout_centerHorizontal="true"
            android:gravity="center"
            android:layout_alignParentLeft="true"
            android:layout_margin="2dp"
            android:textStyle="bold" />

        <RelativeLayout
            android:id="@+id/rlLogout"
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:layout_margin="5dp"
            android:background="@drawable/exit" />

    </RelativeLayout>

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

It should look like this snap [XML View]

enter image description here

BUT IT LOOKS LIKE BELOW which I don't want

enter image description here

If I take it in single line, it shows text title like this text... which is also invalid. I know this is issue of Navigation icon but don't know how to fix it? Please correct me if anything is wrong in xml.

Cœur
  • 37,241
  • 25
  • 195
  • 267
VVB
  • 7,363
  • 7
  • 49
  • 83
  • 1
    Your description along with the screenshots are very unclear... I can't understand what you want to achieve – Drilon Blakqori Dec 06 '16 at 10:08
  • @Can you explain about the need of relative layout - rLogiout ? Also, check these probable duplicates - http://stackoverflow.com/questions/26533510/android-toolbar-center-title-and-custom-font?rq=1, http://stackoverflow.com/questions/31640629/android-how-to-center-a-title-textview-in-a-toolbar?rq=1 – Gurupad Mamadapur Dec 06 '16 at 10:09
  • @VVB Have you tried my answer – Ravish Sharma Dec 06 '16 at 13:14

3 Answers3

1

Do not use getSupportActionBar().setTitle(title); to set the title, if you have a custom Toolbar layout.

Instead, assuming your XML layout looks like this:

    <!-- Toolbar -->
    <android.support.v7.widget.Toolbar
        android:id="@+id/main_toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <LinearLayout
            android:id="@+id/main_toolbar_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:orientation="vertical">

            <TextView
                android:id="@+id/main_toolbar_title"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />

        </LinearLayout>

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

You need to call, somewhere in your MainActivity's onCreate() probably:

((TextView) findViewById(R.id.main_toolbar_title)).setText("Title!");

Ravish Sharma
  • 207
  • 2
  • 14
0

Try giving minimum height attribute yo your Textview

android:minHeight="0dp"
Rahul
  • 189
  • 1
  • 13
0

Hey try this code it will work for sure

  <android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:background="@color/colorPrimary"
    app:elevation="8dp"
    app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">




    <TextView
        android:id="@+id/toolbar_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toLeftOf="@+id/rlLogout"
        android:text="Long text title"
        android:textColor="#000000"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:fontFamily="sans-serif-light"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true"
        android:layout_gravity="center"
        android:layout_alignParentLeft="true"
        android:layout_margin="2dp"
        android:textStyle="bold" />


    <ImageView
        android:id="@+id/logout"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="end"
        android:layout_marginRight="10dp"
        android:layout_marginEnd="10dp"
        android:scaleType="center"
        android:src="@drawable/hamburger_menu" />

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

it looks like this

Nitesh
  • 318
  • 3
  • 16