How to change ActionMode background color in Android

江枫思渺然 提交于 2019-11-27 04:31:49
Shayan Pourvatan

if you want change the color of ActionBar just do this:

 ActionBar bar = getActionBar();
 bar.setBackgroundDrawable(new ColorDrawable("COLOR"));

see the following link for more info

and if you using ActionMode This is the style used for any ActionMode. You'll need to create your own style to customize it

<style name="Widget.ActionMode">
<item name="android:background">?android:attr/actionModeBackground</item>
<item name="android:backgroundSplit">?android:attr/actionModeSplitBackground</item>
<item name="android:height">?android:attr/actionBarSize</item>
<item name="android:titleTextStyle">@android:style/TextAppearance.Widget.ActionMode.Title</item>
<item name="android:subtitleTextStyle">@android:style/TextAppearance.Widget.ActionMode.Subtitle</item>
</style>

more info in this site

see this too

Edit

for pre-Honeycomb see this please

maybe this or this helped you

Kokusho

In my case i resolved with this. First i created a style to define the actionModeBackground:

<style name="MyTheme.ActionMode" parent="@android:style/Theme.Holo.Light">
    <item name="android:actionModeBackground">#FFFFFF</item>
</style>

Then i add the style into my base application theme:

<style name="AppTheme" parent="AppBaseTheme">
    <item name="android:actionModeStyle">@style/MyTheme.ActionMode</item>
</style>

Hope it helps!!!

Chintan Khetiya

If you are using simple Action Bar then try this,

ActionBar abar = getActionBar();
abar.setBackgroundDrawable(new ColorDrawable(0xff123456));

But if you are using Actionbar share lock Lib then try this

getSupportActionBar().setBackgroundDrawable(new ColorDrawable(0xff123456));

ColorDrawable(0xff123456) - should be your color code.

Drawable/styles.xml

 <item name="android:actionModeBackground">@drawable/actionbar_background</item>
 <item name="actionModeBackground">@drawable/actionbar_background</item>

Change Theme as per API level

If you want to change as per the API level then you should go for this

Colateral

If you want to do it programmatically (supporting AppCompat) you need to look for icon parent view. This code is tested from 2.3.7 to 6.0.1. You need to check on lower API... .

first get your activity decor view

final ViewGroup  decorView = (ViewGroup) getActivity().getWindow().getDecorView();

second set the color in onPrepareActionMode. You can also set the back icon or other elements here

@Override
public boolean onPrepareActionMode(ActionMode actionMode, Menu menu) 
{

     decorView.postDelayed(new Runnable() {

     @Override
     public void run() 
     {
        int buttonId = getResources().getIdentifier("action_mode_close_button", "id", "android");

        View v = decorView.findViewById(buttonId);
        if (v == null)
        {
           buttonId = R.id.action_mode_close_button;
            v = decorView.findViewById(buttonId);   
        }

        if (v != null)
        {                         
           ((View)v.getParent()).setBackgroundColor(Color.red /*your color here*/);
        }               
     }   
     }, 500);}

Override this style in your custom theme:

<style name="Base.Widget.AppCompat.ActionMode" parent="">
    <item name="background">?attr/actionModeBackground</item>
    <item name="backgroundSplit">?attr/actionModeSplitBackground</item>
    <item name="height">?attr/actionBarSize</item>
    <item name="titleTextStyle">@style/TextAppearance.AppCompat.Widget.ActionMode.Title</item>
    <item name="subtitleTextStyle">@style/TextAppearance.AppCompat.Widget.ActionMode.Subtitle</item>
    <item name="closeItemLayout">@layout/abc_action_mode_close_item_material</item>
</style>

For example:

<style name="LywActionMode" parent="Base.Widget.AppCompat.ActionMode">
    <item name="background">@color/lyw_primary_color</item>
    <item name="backgroundSplit">@color/lyw_primary_color</item>
</style>

<style name="LywCompatTheme" parent="@style/Theme.AppCompat.Light">
    ...
    <item name="actionModeStyle">@style/LywActionMode</item>
</style>

This is the style used for any ActionMode, I pulled it from the SDK. You'll need to create your own style to customize it. It's really easy to do. If you've never done anything like this before, you should read through this post on customizing the ActionBar. It explains everything you'll need to know.

<style name="Widget.ActionMode">
<item name="android:background">?android:attr/actionModeBackground</item>
<item name="android:backgroundSplit">?android:attr/actionModeSplitBackground</item>
<item name="android:height">?android:attr/actionBarSize</item>
<item name="android:titleTextStyle">@android:style/TextAppearance.Widget.ActionMode.Title</item>
<item name="android:subtitleTextStyle">@android:style/TextAppearance.Widget.ActionMode.Subtitle</item>
</style>

The simplest way to do it

ActionBarContextView actionBar = getWindow().getDecorView().findViewById(R.id.action_mode_bar);
    actionBar.setBackgroundColor(WhateverColorYouWant);

I wanna make more clearer this answer. First create custom background "storage_list_header_shape.xml" in your drawable folder.

<?xml version="1.0" encoding="utf-8"?>
  <shape xmlns:android="http://schemas.android.com/apk/res/android"
     android:shape="rectangle" >

   <gradient
      android:angle="270"
      android:startColor="@android:color/holo_orange_light"
      android:centerColor="@android:color/holo_orange_dark"
      android:endColor="@android:color/holo_orange_light" />

   <size android:height="3dp" />

 </shape>

Second, create custom style in your style.xml

<style name="customActionModeTheme" parent="@android:style/Theme.Holo.Light">
    <item name="android:actionModeBackground">@drawable/storage_list_header_shape</item>
</style>

Third, call the style in your manifest.

<activity
     android:name="com.javacafe.activities.Main"
     android:launchMode="singleTop"
     android:screenOrientation="sensorLandscape"
     android:theme="@style/customActionModeTheme" >
</activity>

For those who still struggle, and none of the existing solutions helps, please make sure that in your layout with toolbar you have this:

app:popupTheme="@style/Theme.ToolBar"
android:popupTheme="@style/Theme.ToolBar"

and you don't have

app:theme="@style/Theme.ToolBar"

Since they will rise a conflict and your customization will not work.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!