Reputation: 2287
I have read about every answer on this and still can't seem to make it work. When I call startSupportActionMode
, the action mode menu is stacked on top of the toolbar instead of replacing it. This graphic is from another user that was having the same issue.
Most of the other questions were resolved by using windowActionModeOverlay
in the styles. This doesn't seem to be working for me. I'm also sure I'm importing the correction action mode.
(import android.support.v7.view.ActionMode;
)
My situation is a little different. My main activity is a AppCompatActivity
. I load different fragments based on choices made from a navigation drawer. The actionbar is changed based on the navigation item selected.
This code all worked before I changed over to appCompat-v7
. I am trying to convert to the new material design and am therefor replacing the base actionbar with a toolbar. Here are some code snippets:
Manifest:
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme"
android:name="com.acme.common.MINApplication"
android:largeHeap="true">
<activity android:name="com.acme.common.MINMainActivity"
android:theme="@style/AppTheme"
android:configChanges="orientation|screenSize|keyboardHidden" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
Styles:
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Main theme colors -->
<!-- your app branding color for the app bar -->
<item name="android:colorPrimary">@color/ColorPrimary</item>
<!-- Support library compatibility -->
<item name="colorPrimary">@color/ColorPrimary</item>
<!-- darker variant for the status bar and contextual app bars -->
<item name="android:colorPrimaryDark">@color/ColorPrimaryDark</item>
<!-- Support library compatibility -->
<item name="colorPrimaryDark">@color/ColorPrimaryDark</item>
<!-- theme UI controls like checkboxes and text fields -->
<item name="android:statusBarColor">@android:color/transparent</item>
<item name="windowActionModeOverlay">true</item>
<item name="android:windowActionModeOverlay">true</item>
</style>
Main Activity:
public class MINMainActivity extends AppCompatActivity
{
private final String TAG = ((Object) this).getClass().getSimpleName();
private View mMainView;
// New Toolbar and Navigation View
public Toolbar toolbar;
public RecyclerView recyclerView;
private MINPageAdapter adapter;
private RecyclerView.LayoutManager layoutManager;
public DrawerLayout drawerLayout;
public android.support.v7.app.ActionBarDrawerToggle drawerToggle;
volatile public boolean isDrawerOpen = false;
private Menu mMenu;
@Override
public void onCreate(Bundle savedInstanceState)
{
supportRequestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS | Window.FEATURE_ACTION_BAR_OVERLAY);
super.onCreate(savedInstanceState);
// Inflate main view
mMainView = getLayoutInflater().inflate(R.layout.material_design_drawer_layout, null);
setContentView(mMainView);
sharedInstance = this;
// Setup New Toolbar implementation
setupToolbar();
// Setup Recycler Adapter
setupAdapter();
// Setup Navigation View
initNavigationDrawer();
// Show startup screen/fragment
showStartupScreen();
// Color components based on json settings
setCustomAttributes();
init();
}
Fragment:
public class MINPageTypeGridFragment extends Fragment
{
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setRetainInstance(true);
setHasOptionsMenu(true);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
// Inflate view
gridView = (StickyGridHeadersGridView)inflater.inflate(R.layout.page_gridview_fragment, container, false);
// Set up specific controls/views
initializeGridView(inflater, container);
...
return gridView;
}
@Override
public void onResume()
{
ActionBar actionBar = MINMainActivity.getSharedInstance().getSupportActionBar();
if(actionBar != null)
{
actionBar.show();
actionBar.setDisplayOptions(ActionBar.DISPLAY_HOME_AS_UP |
ActionBar.DISPLAY_SHOW_HOME |
ActionBar.DISPLAY_USE_LOGO |
ActionBar.DISPLAY_SHOW_TITLE);
actionBar.setTitle(pageDefinition.pageName);
}
...
super.onResume();
}
@Override
public boolean onOptionsItemSelected(MenuItem item)
{
int count = 0;
...
freezeGridView();
mActionMode = MINMainActivity.getSharedInstance().startSupportActionMode(mActionModeCallback);
...
return super.onOptionsItemSelected(item);
}
public ActionMode.Callback mActionModeCallback = new ActionMode.Callback()
{
@Override
public boolean onCreateActionMode(ActionMode actionMode, Menu menu)
{
MenuItem shareMenuItem;
...
actionMode.getMenuInflater().inflate(R.menu.gridview_edit_menu, menu);
shareMenuItem = menu.findItem(R.id.MenuItemShare);
mShareActionProvider = (ShareActionProvider) MenuItemCompat.getActionProvider(shareMenuItem);
if(mShareActionProvider != null)
{
mShareActionProvider.setShareIntent(Share(null));
mShareActionProvider.setOnShareTargetSelectedListener(new ShareActionProvider.OnShareTargetSelectedListener()
{
@Override
public boolean onShareTargetSelected(ShareActionProvider shareActionProvider, Intent intent)
{
if(mActionMode != null)
{
currentMode = MODE_STANDARD;
clearSelectedItems();
mActionMode.finish();
mActionMode = null;
}
return false;
}
});
...
return true;
}
@Override
public boolean onPrepareActionMode(ActionMode actionMode, Menu menu)
{
return false;
}
@Override
public boolean onActionItemClicked(ActionMode actionMode, MenuItem menuItem)
{
switch(menuItem.getItemId())
{
case R.id.MenuItemEdit:
launchAlbumItemDetails(MINPageTypeGridFragment.this, currentAlbumItem, pageDefinition.pageConfigFileName);
return true;
case R.id.MenuItemDelete:
deleteItem(MINPageTypeGridFragment.this, currentAlbum, currentAlbumItem);
return true;
default:
return false;
}
}
@Override
public void onDestroyActionMode(ActionMode actionMode)
{
clearSelectedItems();
}
}
I'm sure that I'm missing something obvious but I'm out of ideas.
Upvotes: 2
Views: 1211
Reputation: 5773
You should remove 'duplicate' definition: android:windowActionModeOverlay -> windowActionModeOverlay; android:colorPrimary -> colorPrimary and so on. This is an example that should work:
<style name="AppBaseTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="windowActionModeOverlay">true</item>
<item name="colorControlNormal">@color/appColorControlNormal</item>
<item name="colorControlHighlight">@color/appColorControlHighlight</item>
<item name="colorControlActivated">@color/appColorControlActivated</item>
<item name="colorPrimary">@color/appColorPrimary</item>
<item name="colorPrimaryDark">@color/appColorPrimaryDark</item>
<item name="colorAccent">@color/appColorAccent</item>
</style>
Upvotes: 3
Reputation: 13548
Be sure to set the toolbar as current actionbar (maybe your method setupToolbar
already do this):
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
After that, change your code and start the action mode directly from the toolbar reference:
toolbar.startActionMode(mActionModeCallback)
Let us know if this fix your problem.
Upvotes: 0