Android Toolbar: center title

Asked

Viewed 4,578 times

5

I’m trying to center the title of mine Toolbar but I can’t.

I’ve tried to use Theme (did not work) and I have tried in Java code (however in Java code as I am using the API 13, can’t use the .setTextAligment). I’ve also tried using getSupportActionBar().setDisplayOptions(toolbar.TEXT_ALIGNMENT_CENTER) which also did not work.

I don’t have any more ideas...

Styles.xml

<resources>

    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    </style>
    <style name="AppTheme.Toolbar.Title" parent="Theme.AppCompat.NoActionBar">
        <!-- Set proper title size -->
        <item name="android:textSize">@dimen/abc_text_size_title_material_toolbar</item>
        <item name="android:gravity">center</item>
        <item name="android:layout_gravity">center</item>
    </style>
</resources>

xml layout.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/yellow0"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <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/toolbar"
        android:layout_gravity="center"
        android:gravity="center"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:minHeight="?attr/actionBarSize"
        android:background="@drawable/toolbar_main">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="@color/brown"
            android:textSize="16sp"
            android:textStyle="bold"
            android:gravity="center"
            android:id="@+id/toolbar_title"/>

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

    <LinearLayout
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:gravity="center"
        android:layout_gravity="center"
        android:paddingTop="70dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <Button
            android:id="@+id/buttonMesActual"
            android:layout_marginTop="20dp"
            android:gravity="center"
            android:textColor="@color/brown"
            android:layout_width="200dp"
            android:layout_height="wrap_content"
            android:background="@drawable/buttons"
            android:text="Botão1"
            android:layout_marginBottom="10dp"
            android:onClick="onClick"/>

        <Button
            android:id="@+id/buttonInserir"
            android:layout_width="200dp"
            android:layout_height="wrap_content"
            android:background="@drawable/buttons"
            android:text="Botão2"
            android:textColor="@color/brown"
            android:layout_marginBottom="10dp"
            android:onClick="onClick"/>

        <Button
            android:textColor="@color/brown"
            android:id="@+id/buttonMessPassados"
            android:layout_width="200dp"
            android:layout_marginBottom="10dp"
            android:layout_height="wrap_content"
            android:background="@drawable/buttons"
            android:text="Botão3"
            android:onClick="onClick"/>

        <Button
            android:background="@drawable/buttons"
            android:layout_width="200dp"
            android:layout_height="wrap_content"
            android:id="@+id/buttonDefinirM"
            android:textColor="@color/brown"
            android:text="Botão4"
            android:onClick="onClick"/>

    </LinearLayout>

    <TableLayout
        android:layout_width="match_parent"
        android:layout_gravity="bottom|center"
        android:gravity="bottom|center"
        android:layout_height="wrap_content"
        android:paddingTop="40dp"
        android:orientation="vertical">

        <TableRow android:gravity="center"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">

            <TextView
                android:textStyle="bold"
                android:id="@+id/textMesActual"
                android:text="Text2:"
                android:textColor="@color/brown"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />

            <TextView
                android:id="@+id/textMes"
                android:textColor="@color/brown"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />
        </TableRow>

        <TableRow android:gravity="center"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">

            <TextView
                android:textStyle="bold"
                android:textColor="@color/brown"
                android:id="@+id/textDespesas"
                android:text="Text1:"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />

            <TextView

                android:id="@+id/textDesp"
                android:textColor="@color/brown"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />
        </TableRow>

        <TableRow android:gravity="center"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">

            <TextView
                android:textStyle="bold"
                android:id="@+id/textMeta"
                android:text="Text0: "
                android:textColor="@color/brown"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />

            <TextView
                android:id="@+id/textMetaDef"
                android:textColor="@color/brown"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />
        </TableRow>
    </TableLayout>
</LinearLayout>

toolbar_main.xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape android:shape="rectangle">
            <solid android:color="@color/white" />
        </shape>
    </item>
    <item android:bottom="2dp">
        <shape android:shape="rectangle">
            <gradient android:angle="-90" android:startColor="#fdeec1" android:endColor="#f9d35e"  />

        </shape>
    </item>
</layer-list>
  • Have you tried to set your style with the attribute app:theme?

  • @sicachester, last commentator pf

  • 1

    Comment on sicachester’s reply, or it will not be notified.

  • @bfavaretto comment here because below when typing user_name then does not appear to me

  • 1

    The system removes @user because it is unnecessary there (the answer is his, he is notified of all comments). Already here, as he is not in this "conversation" (no comment), does not receive any notification (nor with @). See http://meta.pt.stackoverflow.com/questions/943/

2 answers

3


Remember that the Toolbar is a ViewGroup, that is, you can put components inside it without any problem.

To place a text in the center, place a TextView as the attribute android:gravity="center":

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

    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:text="Seu titulo"
        android:textSize="16sp" />

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

Just one important detail that I noticed in your layout: use the integer that is on AppCompat called ?attr/actionBarSize to set the size of your Toolbar. Will apply the standard and correct size to your Toolbar

Refrencia: http://developer.android.com/reference/android/widget/Toolbar.html

@Edit

In its layout, the TextView is as android:layout_width="wrap_content" and android:layout_height="wrap_content". Leave as match_parent and it will work

  • I’ve tried this solution before, but it doesn’t work. The name of the Application appears and then the title of Textview (from Toolbar), I also took the label in the manifest, but it does not work. I have also tried in Java to make Toolbar.setTitle(null) and only then make a setTitle, but none of this works :\

  • @porthfind you removed the tag in the manifest or left as "" (empty)?

  • I had deleted the label and I saw that was the problem, I already put it empty and at least this part is already well, but the text continues to appear without being centered, will it be that I have the Toolbar within a Linearlayout? I updated the question with the layout to be easier.

  • @porthfind Can remove yes, no need to put the Toolbar within another ViewGroup, and also you are giving re-drawn from background (since you paint the LinearLayoutand soon after its Toolbar)

  • I can’t take out Linearlayout because I have other components, I thought I could.. I updated my question with the full layout, will be q can see if there is something wrong q is preventing center text?

  • @porthfind what’s in the file toolbar_main ?

  • It is to draw a white line at the end of Toolbar and put degrade in background color. updated question with toolbar_main code

  • @porthfind edited! See if it solves

  • 1

    Thank you! It worked! D

  • @porthfind great! Any question just ask

Show 5 more comments

0

My opinion is that you do not seek to put the text this way because it will not according to the specifications of spaces and positioning defined here.

Always try to follow the guidelines and will see that will make an app more easily and high quality

Browser other questions tagged

You are not signed in. Login or sign up in order to post.