aiueoH
aiueoH

Reputation: 783

ActionMode background doesn't work with set support ActionBar with Toolbar

My action mode background in style doesn't work since i use ToolBar to be ActionBar.

part of MainActivity

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    final Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    startActionMode(new ActionMode.Callback() {
        @Override
        public boolean onCreateActionMode(ActionMode actionMode, Menu menu) {
            return true;
        }

        @Override
        public boolean onPrepareActionMode(ActionMode actionMode, Menu menu) {
            return true;
        }

        @Override
        public boolean onActionItemClicked(ActionMode actionMode, MenuItem menuItem) {
            return true;
        }

        @Override
        public void onDestroyActionMode(ActionMode actionMode) {

        }
    });
}

layout

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.dowob.testactionmodestyle.MainActivity">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            >
        </android.support.v7.widget.Toolbar>
    </android.support.design.widget.AppBarLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Hello World!"
            android:id="@+id/textView"/>

    </LinearLayout>
</LinearLayout>

part of manifest

<activity android:name=".MainActivity"
    android:theme="@style/MainActivityTheme">
    <intent-filter>
        <action android:name="android.intent.action.MAIN"/>

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

styles.xml

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

    <style name="MainActivityTheme" parent="AppTheme">

        <item name="windowNoTitle">true</item>
        <item name="windowActionBar">false</item>
        <item name="android:windowFullscreen">true</item>
        <item name="android:windowContentOverlay">@null</item>

        <item name="colorPrimary">#FF0000</item>
        <item name="colorPrimaryDark">#FF00FF</item>

        <item name="actionModeStyle">@style/ActionMode</item>
        <item name="android:actionModeStyle">@style/ActionMode</item>
    </style>

    <style name="ActionMode" parent="Widget.AppCompat.ActionMode">
        <item name="android:background">@color/actionModeBg</item>
        <item name="background">@color/actionModeBg</item>
    </style>

</resources>

I've tried this solution but still not work.

This is my whole project.

Does anyone know how to solve it?? Thanks!

Upvotes: 1

Views: 636

Answers (2)

Dimezis
Dimezis

Reputation: 1595

In my case the problem was in Toolbar theme. It's weird, but the style for ActionMode doesn't work if Toolbar theme is Theme.AppCompat.(any), not ThemeOverlay.AppCompat.(any).

Maybe it will help someone.

Upvotes: 1

aiueoH
aiueoH

Reputation: 783

i solve this problem by my coworker, just remove the theme of AppBarLayout them it work fine.

Before:

<android.support.design.widget.AppBarLayout
        android:id="@+id/appbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme">

After :

<android.support.design.widget.AppBarLayout
        android:id="@+id/appbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

Upvotes: 1

Related Questions