LoginSignup
2
1

More than 5 years have passed since last update.

[Android] How To Change "ActionBar" Title Text Color when applied "Theme.MaterialComponents.Light.DarkActionBar" to AppTheme

Last updated at Posted at 2018-10-29

At First

Caution: This article is sensitive because it may depends on Android SDK Version or design support library com.android.support:design version.

Confusing from many solutions on web.

I searched many article by google but cannot resolved.
Finally I got a solution.

Preconditions

I tried to set to app/build.gradle file like this.

apply plugin: 'com.android.application'

apply plugin: 'kotlin-android'

apply plugin: 'kotlin-android-extensions'

android {
    compileSdkVersion 28
    defaultConfig {
        applicationId "com.example.actionbartextcolor"
        minSdkVersion 26
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation"org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation 'com.android.support:appcompat-v7:28.0.0'
    implementation 'com.android.support:design:28.0.0'
    implementation 'com.android.support.constraint:constraint-layout:1.1.3'
}

The Nex Sample cannot apply the custom color to ActionBar in SDK Lv28. this way less than SDK Lv28.

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.MaterialComponents.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
        <item name="actionBarStyle">@style/MyActionBar</item>
    </style>

   <!-- this cannot apply textColor -->

    <style name="MyActionBar"
        parent="@style/Widget.AppCompat.ActionBar">
        <item name="android:titleTextStyle">@style/TitleTextStyle</item>
        <item name="android:background">@color/colorPrimary</item>
        <item name="android:backgroundStacked">@color/colorPrimary</item>
        <item name="android:backgroundSplit">@color/colorPrimary</item>

        <!-- Support library compatibility -->
        <item name="titleTextStyle">@style/TitleTextStyle</item>
        <item name="background">@color/colorPrimary</item>
        <item name="backgroundStacked">@color/colorPrimary</item>
        <item name="backgroundSplit">@color/colorPrimary</item>
    </style>

    <style name="TitleTextStyle"
        parent="@style/TextAppearance.AppCompat.Widget.ActionBar.Title">
        <item name="android:textColor">@color/colorAccent</item>
    </style>
</resources>

Right way is Next.

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.MaterialComponents.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
        <item name="toolbarStyle">@style/CustomToolBarStyle</item>
    </style>

    <!-- ActionBar is no longer Toolbar -->

    <style name="CustomToolBarStyle" parent="@style/Widget.AppCompat.Toolbar">
        <item name="titleTextColor">@color/colorAccent</item>
    </style>

</resources>

How did I get it?

I got ActionBar Instance's Class with Debugger.
And I researched who have title text.
The Answer is android.support.v7.widget.Toolbar.
Finally I found the Next code.

        if (a.hasValue(styleable.Toolbar_titleTextColor)) {
            this.setTitleTextColor(a.getColor(styleable.Toolbar_titleTextColor, -1));
        }

The teachings from attaching style

Do not use google.
Just run.

2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1