'How do I change the color of words in AppBar title separately

I'm making a simple home assistant app and I want to display connection status in AppBar title. I want to have one part of the title colored in light grey and the second part colored in green if the bridge is connected or in red, if the bridge is disconnected.

Here's a picture of what I want to achieve:

what I want it to look like

enter image description here

How can I do it? Is this even possible?

I've also posted a code below of what I have for now - this is just a single colored title

activity_main.xml

    <com.google.android.material.appbar.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.0">

        <com.google.android.material.appbar.MaterialToolbar
            android:id="@+id/topAppBar"
            style="@style/AppBar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            app:menu="@menu/top_app_bar"
            app:title="@string/appbar_title"
            app:titleMarginStart="24dp"
            tools:layout_editor_absoluteX="0dp"
            tools:layout_editor_absoluteY="0dp" />

    </com.google.android.material.appbar.AppBarLayout>

themes.xml

    <style name="AppBar" parent="Widget.MaterialComponents.Toolbar.Primary">
        <item name="titleTextColor">@color/light_gray</item>
    </style>


Solution 1:[1]

Use Spans and Spannable strings. See the guide to Spans for further instructions and examples.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 zen_of_kermit