0

I've added the toolbar to my Android app following the guidelines of this page: *https://developer.android.com/training/implementing-navigation/nav-drawer#java * The following xml code was added to my layout xml file:

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

I do get the toolbar on my app. But I really want to move the "title" of the toolbar to the center. The "title" is the name of my app. I want to change its gravity and appearance. So, I tried the following changes to the xml code, but with no success.

<android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            android:text="MySolvingApp"
            android:fontFamily="@font/cutive"
            android:textAlignment="center"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:theme="@style/ThemeOverlay.AppCompat.ActionBar" />

How can I access this piece of text and change its properties and appearance? Is it possible to access it via the xml layout or via code.

Thanks for any suggestions

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
i_o
  • 777
  • 9
  • 25
  • Possible duplicate of [How to set Toolbar text and back arrow color](https://stackoverflow.com/questions/26969424/how-to-set-toolbar-text-and-back-arrow-color) – Morteza Jalambadani Sep 16 '18 at 09:24
  • @user1506104 I haven't read much about the link. I am looking in the android reference docs for any code under the widget toolbar v7 – i_o Sep 16 '18 at 09:36
  • check my answer below. it worked for me some time ago. – user1506104 Sep 16 '18 at 09:46
  • @user1506104 I have tried your answer but the character > is giving problems. It says that the toolbar widget is not closed in the xml file – i_o Sep 16 '18 at 09:49

3 Answers3

1

I was able to create an answer by doing the following:

toolbar.setTitle("");
toolbar.setBackground(new MyTitleDrawable());

In this drawable class you would create a paint to draw your title. The paint can be customized to your liking, text size, stroke width, typeface etc.

Make sure you also create an int representing the color "colorPrimary" from the appTheme file under resources, in order to keep up with material design guidelines.

color = context.getResources().getColor(R.color.colorPrimary,null);

inside onDraw method:

//draw the title on the center of canvas
canvas.translate(canvas.getWidth()/2f , canvas.getHeight()/2f);
//Don't forget to measure your text title with a Rect object

paint.getTextBounds(title,0,title.length(),rect);

//now draw your title at the origin which is the center of the canvas.
//draw it right at 0 , 0  and take into account rect.width()

canvas.drawText(title,0 - rect.width()/2f , 0 + rect.height() / 2f , paint);

after doing this, you will get a nice toolbar where the title has been fully customized to your liking and it is positioned right in the center of the toolbar.

i_o
  • 777
  • 9
  • 25
0

I remember this worked for me a long time ago.

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

    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="Toolbar text"
        android:gravity="center"/>

</android.support.v7.widget.Toolbar>
user1506104
  • 6,554
  • 4
  • 71
  • 89
0

you can add this

<android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        android:theme="@style/ThemeOverlay.AppCompat.ActionBar" >
  <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text=your text/>
</android.support.v7.widget.Toolbar

or from manifest.xml like this

<activity android:name="yourActivity"
        android:label=""/>
Mohammad Rbabah
  • 456
  • 2
  • 10
  • the following character is giving problems ">" It says the widget is not closed – i_o Sep 16 '18 at 09:39