Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to specify dark action mode with my theme

I know there are a couple of questions about styling the contextual action bar (ActionMode) piece of the action bar, but they don't quite seem to address what I'm after.

I'm using the Toolbar with a light theme and dark action bar. The Toolbar looks like I want it, but the action mode looks like the regular dark theme. What do I need to change in my style to get the dark themed action mode (not just action bar)? It seems I should be able to do this quickly by tapping into Theme.AppCompat since that shows the CAB how I want it, but I don't want the rest of the application to be dark.

I'm only concerned about API 14+ and am using the support Toolbar in place of action bar.

Here is my base style

<style name="AppTheme.Base" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="android:actionModeBackground">@color/colorActionMode</item>
    <item name="android:windowActionModeOverlay">true</item>
</style>

Toolbar style

<style name="AppTheme.Toolbar" parent="ThemeOverlay.AppCompat.Dark.ActionBar">
    <item name="android:textColorPrimary">@color/abc_primary_text_material_dark</item>
    <item name="actionMenuTextColor">@color/abc_primary_text_material_dark</item>
    <item name="android:textColorSecondary">@color/abc_primary_text_material_dark</item>
    <item name="android:background">@color/colorPrimaryDark</item>
</style>

Toolbar layout file (setting popupTheme here doesn't seem to have any effect).

<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="?android:attr/actionBarSize"
    app:theme="@style/AppTheme.Toolbar"
    app:popupTheme="@style/ThemeOverlay.AppCompat.Dark"
    android:popupTheme="@style/ThemeOverlay.AppCompat.Dark"
    android:elevation="2dp"
    android:focusable="false"/>

Here's my Toolbar (which is how I want it)

styled toolbar

Here's my ActionMode (which I need to invert)

my action mode

Here's what I want the ActionMode to look like (which I got by changing my style to inherit from Theme.AppCompat instead of Theme.AppCompat.Light.DarkActionBar. The problem being that the rest of the application goes dark, which I don't want.

dark action mode

like image 322
Spencer Avatar asked Jan 23 '15 20:01

Spencer


1 Answers

Start with overriding attribute actionModeStyle in your base theme:

<item name="actionModeStyle">@style/LStyled.ActionMode</item>

Define LStyled.ActionMode as:

<style name="LStyled.ActionMode" parent="@style/Widget.AppCompat.ActionMode">
    <item name="titleTextStyle">@style/LStyled.ActionMode.Title</item>
    <item name="subtitleTextStyle">@style/LStyled.ActionMode.Subtitle</item>
</style>

Finally:

<style name="LStyled.ActionMode.Title" parent="@style/TextAppearance.AppCompat.Widget.ActionMode.Title">
    <item name="android:textColor">@color/color_action_mode_text</item>
</style>

<style name="LStyled.ActionMode.Subtitle" parent="@style/TextAppearance.AppCompat.Widget.ActionMode.Subtitle">
    <item name="android:textColor">@color/color_action_mode_text</item>
</style>

This will override theme specified text color(s) for title & subtitle(if needed).

What's left is the overflow button. Fire up any image editing software that supports color-replacement. Use the overflow menu png from AppCompat's res/drawable-XXXX folder. Paint it white. Next, override actionOverflowButtonStyle in your base theme and specify the png you've just modified:

<item name="actionOverflowButtonStyle">@style/LStyled.OverFlow</item>

<style name="LStyled.OverFlow" parent="@style/Widget.AppCompat.ActionButton.Overflow">
    <item name="android:src">@drawable/modified_overflow_icon</item>
</style>

There's not much of an explanation/tutorial that I can provide regarding customization of themes. The only way to get a hang of it is to go through [styles][themes].xml files from the framework. Reading the source code also helps - you'll get to know what attributes are being used and where/if they can be customized.

Point of mention:

I want to be able to extend a style to get the same behavior that's built into the dark theme.

Yes, but this might not be desired. The part above that deals with modifying overflow menu icon can be substituted with an overridden colorControlNormal attribute. TintManager (Link) uses the color value from colorControlNormal to tint the overflow menu drawable (among other drawables). Take a look at the contents of array TINT_COLOR_CONTROL_NORMAL in the source code. But overriding colorControlNormal to fix one drawable may change the overall look & feel of the app for worse.

like image 114
Vikram Avatar answered Sep 21 '22 22:09

Vikram