Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Change text color on AppCompatSpinner in Toolbar?

I have recently updated to API 24 and now I am having a problem with the text color on my AppCompatSpinner inside the Toolbar.

Here is what I am talking about, notice how "Pils" at the very top is black. Pre API 24 this was white. (Also, running this APP on a device which is not on API 24 the text shows up white as it should.)

enter image description here

Here is my build.gradle:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 24
    buildToolsVersion '24.0.0'

    defaultConfig {
        applicationId 'com.baviloworks.braumeister'
        minSdkVersion 17
        targetSdkVersion 24
        versionCode 100
        versionName '1.0.0'
    }

    buildTypes {
        release {
            minifyEnabled true
            shrinkResources true
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:24.0.0'
    compile 'com.android.support:support-v4:24.0.0'
    compile 'com.android.support:design:24.0.0'
    compile 'com.android.support:cardview-v7:24.0.0'
    compile 'com.github.PhilJay:MPAndroidChart:v2.2.3'
}

Here is my style.xml:

    <?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <item name="windowActionBar">false</item>
        <item name="android:windowDrawsSystemBarBackgrounds">true</item>
        <item name="colorPrimaryDark">@color/DarkBlue</item>
        <item name="colorAccent">@color/Green</item>
    </style>

    <style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />

    <style name="customTabText" parent="TextAppearance.Design.Tab">
        <item name="android:textSize">13sp</item>
    </style>
</resources>

and here is the XML if my Toolbar:

<android.support.v7.widget.Toolbar
            android:id="@+id/Toolbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/Blue"
            android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light">

    <android.support.v7.widget.AppCompatSpinner
        android:id="@+id/Recipe_Selection_Spinner"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:visibility="gone"
        android:popupBackground="@drawable/spinner_rounded_corners"
        android:minWidth="175dp"/>
</android.support.v7.widget.Toolbar>

It would be great if someone could point me in the right direction!

Thanks :)

I have now tried this:

cursorAdapter = new SimpleCursorAdapter(getActivity().getApplicationContext(), R.layout.custom_spinner, dbData, recipeNames, recipeTo, 0);
cursorAdapter.setDropDownViewResource(R.layout.spinner_dropdown_item);
recipeLoadSelection.setAdapter(cursorAdapter);

and this is the custom spinner:

<TextView xmlns:android="http://schemas.android.com/apk/res/android"        
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textColor="#ffffff" />

But now the text won't show up at all

like image 423
Bavilo Avatar asked Jun 27 '16 19:06

Bavilo


1 Answers

a) Whatever you do do not use application context to inflate views. If you're using SimpleCursorAdapter it uses the context from constructor to inflate views internally.

b) Use the themed context of your toolbar for widgets inside it. Your toolbar overrides android:theme attribute. You'll want to use this context to inflate any views within that toolbar.

Meaning instead of getActivity().getApplicationContext() write getSupportActionBar().getThemedContext() and it will just work. This is the correct approach, it does not hardcode text color.

like image 111
Eugen Pechanec Avatar answered Nov 07 '22 06:11

Eugen Pechanec