How to change the title name in the toolbar? And how do I center it?

1

I would like to know how to change the title of each activity (I have eight different activities) in the toolbar.

I have created a toolbar.xml

<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorPrimary"
android:elevation="4dp"
android:theme="@style/ThemeOverlay.AppCompat.Light"
app:popupTheme="@style/Popup">

where I use include

<include layout="@layout/toolbar"
    android:id="@+id/toolbar" />

in each xml of the activity where I want to use my toolbar.

Then also how to focus on that title. Please, is urgent.

Thanks in advance.

    
asked by user9524937 21.03.2018 в 20:45
source

2 answers

0

Add the title directly in the Toolbar using the property

app:title="titulo toolbar"

for example:

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

or programmatically:

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar_actionbar);            
setSupportActionBar(toolbar);
getSupportActionBar().setTitle("titulo toolbar");

Update:

Since the layouts of your Activity you are loading a include which has a Toolbar defined:

<include layout="@layout/toolbar"
    android:id="@+id/toolbar" />

Center Text in Toolbar:

If you want to center the text within your Toolbar you could do it by adding a TextView within your Toolbar , and define the property android:layout_gravity="center_horizontal" :

<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:background="@color/colorPrimary"
     android:elevation="4dp"
     android:theme="@style/ThemeOverlay.AppCompat.Light"
     app:popupTheme="@style/Popup">

            <TextView            

                android:layout_width="match_parent"

                android:layout_height="match_parent"
                android:text="titulo Toolbar!!!"
                android:gravity = "center"
                android:id="@+id/toolbar_title" />

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

Here is an answer similar on the English site

Change text in Toolbar:

To change the text look for the TextView reference and change the text using the setText()

method
TextView titleToolbar = findViewById(R.id.toolbar_title);
titleToolbar.setText("titulo toolbar");

    
answered by 21.03.2018 / 22:14
source
0

What I do to center the title on the toolbars is the following

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

        <TextView
            android:id="@+id/textView_toolbar"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"   //esta es la linea importante
            android:text="TU_TITULO" />

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

What I do is add a textview within toolbar and that textview with property layout_gravity , center it. Then in your case, if you have it in an include, you can access it in a programmatic way as follows

TextView tituloToolbar = findViewById(R.id.textView_toolbar)
tituloToolbar.setText("aca el titulo de tu activity nuevo");
    
answered by 22.03.2018 в 00:40