Appcompat-v7 update overrides toolbar text color setting

Upgrading support: appcompat-v7: 22.1.1 to insupportable-v7: 23.1.0 caused the title text color setting and support.v7.widget.Toolbar to be canceled .

Why is this happening and how can I fix it?

The toolbar before and after the update is displayed here.

Before updating

After update

Limitations:

  • Use the latest appcompat (currently 23.1.0)

  • Change the toolbar theme, but not the entire app theme

  • minSdkVersion 14

  • MainActivity extends FragmentActivity (I don't want to extend ActionBarActivity)

Updating the appcompat version seems to reverse the effect of textColorPrimary and textColorSecondary (see styles.xml ).

The only difference in code happens in appcompat-v7 and of course compileSdkVersion and targetSdkVersion (see two build. Gradle (Module: app) files below)

Here are all the required code files to test two situations:

MainActivity.java

package com.example.myapplication;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v7.widget.Toolbar;

public class MainActivity extends FragmentActivity {
    private Toolbar toolbar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);
        toolbar = (Toolbar) findViewById(R.id.tool_bar);
        toolbar.setTitle(R.string.app_name);
        toolbar.inflateMenu(R.menu.menu_main)
    }
}

      

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/tool_bar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/colorPrimary"
        android:minHeight="?attr/actionBarSize"
        app:theme="@style/ToolbarTheme" />

</RelativeLayout>

      

styles.xml

<resources>

    <style name="AppTheme" parent="Theme.AppCompat.Light">
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>

    <style name="ToolbarTheme" parent="ThemeOverlay.AppCompat.Dark">
        <item name="android:textColorPrimary">@android:color/white</item>
        <item name="android:textColorSecondary">@android:color/white</item>
    </style>

</resources>

      

menu_main.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <item
        android:title="Refresh"
        app:showAsAction="collapseActionView" />
</menu>

      

colors.xml

<resources>
    <color name="colorPrimary">#3F51B5</color>
    <color name="colorPrimaryDark">#303F9F</color>
    <color name="colorAccent">#FF4081</color>
</resources>

      

AndroidManifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.myapplication" >

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme" >
        <activity android:name=".MainActivity" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

      

build.gradle (module: app) THIS OLD VERSION WORKS!

apply plugin: 'com.android.application'

android {
    compileSdkVersion 22
    buildToolsVersion "23.0.1"

    defaultConfig {
        applicationId "com.example.myapplication"
        minSdkVersion 14
        targetSdkVersion 22
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:22.1.1'
}

      

build.gradle (Module: app) THIS NEW VERSION DOES NOT WORK !!

apply plugin: 'com.android.application'

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.1"

    defaultConfig {
        applicationId "com.example.myapplication"
        minSdkVersion 14
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:23.1.0'
}

      

+3


source to share


2 answers


This is how I solved it:

The first thing I tried was setting it to onCreate

toolbar.setTitleTextColor(....);

, it works, but it is not a very good solution.

So I started working with styles and found a solution that works for me:

In styles.xml add style

<style name="AppTheme.Toolbar.Title" parent="TextAppearance.Widget.AppCompat.Toolbar.Title">
    <item name="android:textColor">@android:color/white</item>
</style>

      



Then on the toolbar add the attribute app:titleTextAppearance="@style/AppTheme.Toolbar.Title"

<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
                                   xmlns:app="http://schemas.android.com/apk/res-auto"
                                   android:id="@+id/tool_bar"
                                   android:layout_width="match_parent"
                                   android:layout_height="wrap_content"
                                   android:background="@color/colorPrimary"
                                   android:minHeight="?attr/actionBarSize"
                                   app:theme="@style/ToolbarTheme"
                                   app:titleTextAppearance="@style/AppTheme.Toolbar.Title" />

      

EDIT:

Use android: theme, and if you need to style the menu, use the app: popupTheme

<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
                                   xmlns:app="http://schemas.android.com/apk/res-auto"
                                   android:id="@+id/tool_bar"
                                   android:layout_width="match_parent"
                                   android:layout_height="wrap_content"
                                   android:background="@color/colorPrimary"
                                   android:minHeight="?attr/actionBarSize"
                                   android:theme="@style/ToolbarTheme"
                                   app:popupTheme="@style/MyMenuStyle"
                                    />

      

+1


source


To summarize the solution, these are the complete changes:

In activity_main.xml

<android.support.v7.widget.Toolbar
    android:id="@+id/tool_bar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/colorPrimary"
    android:minHeight="?attr/actionBarSize"
    android:theme="@style/ToolbarTheme" />

      



In styles.xml

 <style name="ToolbarTheme" parent="ThemeOverlay.AppCompat.Dark" />

      

0


source







All Articles