How to access text on toolbar widget in my app?

androidx.appcompat.widget.toolbar example
android.support.v7.widget.toolbar androidx
android custom toolbar layout
android toolbar custom view example
android add button to action bar programmatically
android action bar example source code
how to add title bar in android studio
android-toolbar title

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

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.

Using the App Toolbar, In AppCompat, Toolbar is implemented in the androidx.appcompat.widget. of using the Toolbar widget is that you can place the view anywhere within your layout. android:textColorPrimary is the color of the title text in the Toolbar --> <​item  Toolbar works well with apps targeted to API 21 and above. However, Android has updated the AppCompat support libraries so the Toolbar can be used on lower Android OS devices as well. In AppCompat, Toolbar is implemented in the androidx.appcompat.widget.Toolbar class. There are two ways to use Toolbar: Use a Toolbar as an Action Bar when you

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=""/>

Set up the app bar, For example, the Toolbar widget provides a material design experience on devices Once you set the toolbar as an activity's app bar, you have access to the  An application may choose to designate a Toolbar as the action bar for an Activity using the setSupportActionBar() method. Toolbar supports a more focused feature set than ActionBar. From start to end, a toolbar may contain a combination of the following optional elements: A navigation button.

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>

android.widget.Toolbar, The application may add arbitrary child views to the Toolbar. They will appear at this position within the layout. If a child view's LayoutParams  Now, execute your application and you will see the following output. Many times you see a menu on the right of the toolbar. Now let’s see how you can create this. First create a folder named menu inside res. Inside the menu directory, you need to create a new menu resource file. I have created a file named menu.xml.

Using the Android Toolbar (ActionBar) - Tutorial, Exercise: Add a toolbar to your application. 8.1. Add a This tutorial describes how to use the toolbar widget in your Android application. You can also add a custom view to the action bar, for example, a button or a text field. In the below exmaple I have created Toolbar. Here I have used In activity_main xml.layout a toolbar and TextView, and In MainActivity I have used toast function.

How to change the Toolbar font on Android - Livefront, Top solutions include defining a custom Toolbar layout, setting text… Overflow yields a handful of strategies for changing the font of the Toolbar. text appearance at runtime, or worst of all, iterating through every Toolbar view. Use android:theme NOT style --> <android.support.v7.widget.Toolbar Get the Medium app. Once you set the toolbar as an activity's app bar, you have access to the various utility methods provided by the v7 appcompat support library's ActionBar class. This approach lets you do a number of useful things, like hide and show the app bar. To use the ActionBar utility methods, call the activity's getSupportActionBar() method.

Mac OS X Power Tools, Customize the Toolbar's Appearance Here's one of those hidden features I was window is a clear, capsule-shaped button (often called the toolbar widget). and you'll find that it cycles through various toolbar views: large icons and text, Apple has provided quick access to the System Preferences application in the  If you're still finding the app drawer more irritating than useful, there is a quick trick that will make it disappear. When you open a message thread on your iPhone in iOS 11 or iOS 12, the app drawer will appear at the bottom. Simply tap the App Store icon to the left of the iMessage or Text Message field, and to the right of the camera icon

Comments
  • Possible duplicate of How to set Toolbar text and back arrow color
  • @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
  • check my answer below. it worked for me some time ago.
  • @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
  • the following character is giving problems ">" It says the widget is not closed