Toolbar and Contextual Actionbar with Appcompat-V7

Toolbar and Contextual ActionBar with AppCompat-v7

Update:

Solution: use the windowActionModeOverlay property. Set this in your theme:

<item name="windowActionModeOverlay">true</item>

and the actionmode will be shown over the action bar instead of pushing it down. (If you're not using the latest AppCompat then you need to add the "android:" prefix to the property). It basically lets AppCompat know that you have a toolbar located in the top of the screen and that it should draw the ActionMode on top of it.



Old answer/workaround:

I ran into the same problem. No matter what theme I set, it always pushes down the Toolbar I set as ActionBar. I tried with and without the support library, but it didn't matter.

Unfortunately I was not able to fix it so I have built a workaround instead. In my ActionModeCallback's onCreateActionMode I hide the action bar:

actionBarToolbar.setVisibility(View.GONE);

and in onDestroyActionModeI show it again:

actionBarToolbar.setVisibility(View.VISIBLE);

The hiding/showing happens so quickly it is not noticeable on my test devices. There is of course a downside: although the enter-animation still works, the exit-animation of the contextual action bar gets lost because the Toolbar immediately pops over it. But until we come across a better solution I guess we are stuck with this.


(My Activity is actually extending a custom BaseActivity class which has a method called getActionBarToolbar(), taken from the Google I/O 2014 app source code, so I can easily get fetch the Toolbar:

BaseActivity activity = (BaseActivity) getActivity();
activity.getActionBarToolbar().setVisibility(View.GONE);

Too bad the I/O app does not use the contextual action bar.)

How do Android's new Toolbar and the Contextual Action Bar work together?

You need to set the following in your theme:

<item name="windowActionModeOverlay">true</item>

This will make the action mode overlay for content view, and therefore your Toolbar.

Contextual action bar appears with toolbar

Ok I figured it out, you need to add:

<item name="windowActionModeOverlay">true</item>

To your activity's theme.

See the following link for more information.

Toolbar and Contextual ActionBar with AppCompat-v7

Contextual Action Bar messing with Toolbar actions

I consider it a bug. I have run into it myself, and isolated it. Here is a workaround, assuming you are extending AppCompatActivity:

@Override
public void onSupportActionModeStarted(ActionMode mode) {
super.onSupportActionModeStarted(mode);
rm1.setEnabled(false);
rm2.setEnabled(false);
rm3.setEnabled(false);
}

@Override
public void onSupportActionModeFinished(ActionMode mode) {
super.onSupportActionModeFinished(mode);
rm1.setEnabled(true);
rm2.setEnabled(true);
rm3.setEnabled(true);
}

How to use Contextual Actionbar (CAB) with support.v7.widget.Toolbar and Listview?

Since everything else is working. I bet it is about styling. You can always style all this.

   <style name="AppTheme" parent="Theme.AppCompat">
<!---- other atrributes -->
<!--- changes the action mode style; height, text size, background etc -->
<item name="actionModeStyle">@style/MyActionMode</item>
<!--- changes left icon of that is used to close the action mode -->
<item name="actionModeCloseDrawable">@drawable/ic_back</item>
</style>

<style name="MyActionMode" parent="Widget.AppCompat.Base.ActionMode">
<!--- changes background of the container -->
<item name="background">?attr/colorPrimary</item>
<!--- changes the action mode background when using actionbar is splitted -->
<item name="backgroundSplit">?attr/colorPrimary</item>
<!--- changes height of the actionmode container view -->
<item name="height">@dimen/toolbar_size</item>
<!--- changes text style of the title, create your own, this is the default -->
<item name="titleTextStyle">@style/TextAppearance.AppCompat.Widget.ActionMode.Title</item>
<!--- changes text style of the subtitle, create your own, this is the default -->
<item name="subtitleTextStyle">@style/TextAppearance.AppCompat.Widget.ActionMode.Subtitle
</item>

Contextual Action Bar using AppCompat

Why do you call listView.clearChoices() every time the item checked state changes? I don't see the whole code but it seems like you clear all of the checks and then just check that item.

how to Customize the Contextual Action Bar using appCompat in material design

You can change the ActionMode background through attribute actionModeStyle:

<style name="AppTheme.Base" parent="Theme.AppCompat.Light">
....
....
<item name="actionModeStyle">@style/LStyled.ActionMode</item>
</style>

<style name="LStyled.ActionMode" parent="@style/Widget.AppCompat.ActionMode">
<item name="background">@color/color_action_mode_bg</item>
</style>

You will of course need to define a color named color_action_mode_bg:

<color name="color_action_mode_bg">#009688</color>

There are other things you can change as well. Example:

<item name="titleTextStyle">...</item>
<item name="subtitleTextStyle">...</item>
<item name="height">...</item>

To change text color of SAVE and SAVETO, add the following to AppTheme.Base:

<item name="actionMenuTextColor">@color/color_action_mode_text</item>

Why is Contextual Action Bar getting different style when calling startSupportActionMode inside onCreate method?

Adding the following lines to values/styles.xml / BaseTheme solved the problem:

    <item name="actionBarPopupTheme">@style/ThemeOverlay.AppCompat.Light</item>
<item name="actionBarTheme">@style/ThemeOverlay.AppCompat.Dark.ActionBar</item>

Although I could not figure out what exactly was happening since this themes were already defined on my Toolbar widget and the setSupportActionBar was called before startSupportActionMode.

Maybe the context action bar was being created before the styles from Toolbar widget could be parsed by the setSupportActionBar.



Related Topics



Leave a reply



Submit