Reverse spinner / edittext stuff construction using AppCompat
I am having trouble building a Material Design app with AppCompat v21 theme. First, I have a theme for an application using AppCompat light with a dark action bar.
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Main theme colors -->
<!-- your app branding color for the app bar -->
<item name="colorPrimary">@color/theme_color</item>
<!-- darker variant for the status bar and contextual app bars -->
<item name="colorPrimaryDark">@color/theme_color_dark</item>
<!-- theme UI controls like checkboxes and text fields -->
<item name="colorAccent">@color/theme_color_accent</item>
</style>
The color branding for my application is quite dark, so I am using the DarkActionBar variant. My problem is that I have multiple layouts where I would like to be placed Spinner
and EditText
inside the layout where the background is mine theme_color
which is dark. The result is Spinner/EditText
black on a dark background. How can I force the style / theme of these controls to use the Dark option so that they appear with white text instead of black text. Even worse, I need a spinner to display the white text, but the spinner dropdowns to show the light version.
source to share
I'm using a Spinner inside a toolbar with a dark background and remember that the Google I / O Android app does the same, so I looked at its styles - ActionBarThemeOverlay to be exact.
After I used the attributes below on my theme, the background color of the Spinner ("arrow") changed:
<item name="colorControlNormal">#fff</item>
<item name="colorControlHighlight">#3fff</item>
colorControlHighlight is the color when the user clicks on the Spinner. However, I have not tested the EditText.
source to share
So, you are looking for a way to apply a different theme for the selection of views. There is no xml method only for most of the elements (other than the Toolbar
ones you already know)
You will have to inflate the views manually using ContextThemeWrapper
:
ContextThemeWrapper wrapper = new ContextThemeWrapper(getActivity(),
R.style.Theme_AppCompat); // The dark one
LayoutInflater footPump = LayoutInflater.from(getActivity()).cloneInContext(wrapper);
// Note null as second argument -- if you pass in parent view, theme will
// be taken from the parent
Spinner spinner = (Spinner) footPump.inflate(R.layout.spinner, null);
to have a layout resource for an inflatable device just create an XML file with one view:
<!-- res/layout/spinner.xml -->
<?xml version="1.0" encoding="utf-8"?>
<Spinner xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
source to share