Search code examples
androidkotlinandroid-toolbar

Set toolbar text color in kotlin


I've been trying to change the text colour in the toolbar but I'm not getting anywhere, I have tried many answers from stack overflow but either it does not work or the app crashes on this line setSupportActionBar(toolbar).

here is the Kotlin activity:

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_chat)
    supportActionBar?.setDisplayHomeAsUpEnabled(true)
    supportActionBar?.title = intent.getStringExtra(AppConstants.GROUP_NAME)
}

and the layout:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#EEEEEE"
tools:context=".ChatActivity">

<android.support.v7.widget.RecyclerView
    android:id="@+id/recycler_view_messages"
    android:layout_width="0dp"
    android:layout_height="0dp"
    app:layout_constraintBottom_toTopOf="@id/relativeLayout_message"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />    

Thank you in advance.


Solution

  • If you want to change it on all the application you can use styles as explained in the actionBar styling documentation:

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
        <!-- the theme applied to the application or activity -->
        <style name="CustomActionBarTheme"
               parent="@style/Theme.Holo">
            <item name="android:actionBarStyle">@style/MyActionBar</item>
            <item name="android:actionBarTabTextStyle">@style/MyActionBarTabText</item>
            <item name="android:actionMenuTextColor">@color/actionbar_text</item>
        </style>
    
        <!-- ActionBar styles -->
        <style name="MyActionBar"
               parent="@style/Widget.Holo.ActionBar">
            <item name="android:titleTextStyle">@style/MyActionBarTitleText</item>
        </style>
    
        <!-- ActionBar title text -->
        <style name="MyActionBarTitleText"
               parent="@style/TextAppearance.Holo.Widget.ActionBar.Title">
            <item name="android:textColor">@color/actionbar_text</item>
        </style>
    
        <!-- ActionBar tabs text styles -->
        <style name="MyActionBarTabText"
               parent="@style/Widget.Holo.ActionBar.TabText">
            <item name="android:textColor">@color/actionbar_text</item>
        </style>
    </resources>
    

    On the other hand if you want to customize the action bar depending on the Activity/Fragment/View you are in you should:

    • Change your App style to a variant with no action bar for example "Theme.AppCompat.Light.NoActionBar
    • Add a Toolbar to your activity layout
    • Set that toolbar as the actionBar with setSupportActionBar(toolbar)

    With this done, you can reference the toolbar to change text color and fully customize the ActionBar either from the XML or programatically