Friday, 18 January 2019

Toolbar won't disappear in action mode

I am trying to implement the feature that, when i am long clicking a list item, the action mode shall start and it shall be possible to delete one or more items.
I am starting in the MainActivity DocumentsActivity a search, which starts a Fragment DocumentsFragment with the ListView an their items. The ListAdapter is initialized and set via method call setListAdapter(this.documentsAdapter) in the onCreate in the Fragment. I set various listner on the listview in the onActivityCreated in the Fragment:

public void onActivityCreated(Bundle savedInstanceState) {

    getListView().setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
    getListView().setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
        @Override
        public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
            getListView().setItemChecked(position, true);
            return true; 
    }});
    getListView().setMultiChoiceModeListener(new AbsListView.MultiChoiceModeListener() {
        @Override
        public boolean onCreateActionMode(ActionMode mode, Menu menu) {
            menu.clear();
            ((DocumentsActivity)getActivity()).getMenuInflater().inflate(R.menu.documents_context_menu, menu);
            return true;
        }
    });
    super.onActivityCreated(savedInstanceState);
}

When I long click on a listitem the action mode gets started and the menu documents_context_menuappears to be the action bar. But the problem is, the action bar appears above the toolbar and the toolbar won't disappear (see the picture).

I've tried to call 'getSupportActionBar().hide()' or set it null or even use another style/theme. It all didn't work. Sometimes it happened that the blue toolbar was completely white, but that is all.

I have absolutely no idea why the toolbar won't disappear. May you give some advice?

Thanks in advance!

_____ Update 1 _____

This is the styles.xml

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="android:fitsSystemWindows">true</item>
    <item name="colorAccent">@color/darkblue100</item>
    <item name="android:actionOverflowButtonStyle">@style/ActionButtonOverflow</item>
    <item name="actionOverflowButtonStyle">@style/ActionButtonOverflow</item>
    <item name="android:actionMenuTextColor">@color/black</item>
</style>

And this is how the action bar is set in the Activity:

protected void onCreate(Bundle savedInstanceState) {
    handleIntent(getIntent());
    requestWindowFeature(5);
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_documents);
    initializeNavigationDrawer();
    args = getIntent().getExtras();
    if (findViewById(R.id.container_documents) != null && savedInstanceState == null) {
        showDocumentsFragment();
    }
}

initializeNavigationDrawer is a method from superclass of the Activity

public void initializeNavigationDrawer() {
    String[] menuItems = getResources().getStringArray(R.array.drawer_nav_items);
    mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
    final ListView mDrawerListView = (ListView) findViewById(R.id.left_drawer);
    drawerAdapter = new DrawerAdapter(getApplicationContext(), menuItems);
    if (mDrawerListView != null) {
        mDrawerListView.setAdapter(drawerAdapter);
        mDrawerListView.setOnItemClickListener(new DrawerItemClickListener());
    }
    Toolbar mToolbar = (Toolbar) findViewById(R.id.tool_bar);

    setSupportActionBar(mToolbar);
    if (getSupportActionBar() != null) {
        getSupportActionBar().setDisplayShowCustomEnabled(true);
        ProgressBar progressBar = new ProgressBar(this, null, android.R.attr.progressBarStyleHorizontal);
        progressBar.setVisibility(View.GONE);
        progressBar.setIndeterminate(true);
        getSupportActionBar().setCustomView(progressBar);
        mDrawerToggle = new ActionBarDrawerToggle(
                this,                    /* host Activity */
                mDrawerLayout,           /* DrawerLayout object */
                mToolbar,               /* Toolbar Object */
                R.string.open,           /* "open drawer" description */
                R.string.close           /* "close drawer" description */
        ) {

            public void onDrawerClosed(View view) {
                super.onDrawerClosed(view);
                //getActionBar().setTitle(savedTitle);
                getSupportActionBar().setTitle(savedTitle);
            }

            public void onDrawerOpened(View drawerView) {
                super.onDrawerOpened(drawerView);
                mDrawerListView.bringToFront();
                mDrawerLayout.requestLayout();
                savedTitle = getTitle().toString();
                getSupportActionBar().setTitle(savedTitle);
            }
        };
        mDrawerLayout.addDrawerListener(mDrawerToggle);
        mDrawerToggle.syncState();

        if (getSupportActionBar() != null) {
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
            getSupportActionBar().setDisplayShowTitleEnabled(false);
        }
        mDrawerToggle.setDrawerIndicatorEnabled(false);
        mDrawerToggle.setHomeAsUpIndicator(R.drawable.ic_hamburger);

        mDrawerToggle.setToolbarNavigationClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View View) {
                if (mDrawerLayout.isDrawerOpen(GravityCompat.START)) {
                    mDrawerLayout.closeDrawer(GravityCompat.START);
                } else {
                    mDrawerLayout.openDrawer(GravityCompat.START);
                }
            }
        });
    }

}

_____ Update 2 _____
Android Manifest

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.stack.mhapp">
  <application xmlns:tools="http://schemas.android.com/tools"
    android:name=".data.AppData"
    android:allowBackup="false"
    android:icon="@mipmap/marcel_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme"
    tools:replace="android:icon,android:allowBackup">
    <provider
        android:name="android.support.v4.content.FileProvider"
        android:authorities="com.stack.mhapp.fileprovider"
        android:exported="false"
        android:grantUriPermissions="true">
        <meta-data
            android:name="android.support.FILE_PROVIDER_PATHS"
            android:resource="@xml/filepaths" />
    </provider>
    <activity
        android:name=".documents.DocumentsActivity"
        android:configChanges="screenSize|orientation|keyboardHidden"
        android:label="@string/title_activity_documents">
        <meta-data
            android:name="android.app.searchable"
            android:resource="@xml/searchable" />

        <intent-filter>
            <action android:name="android.intent.action.SEARCH" />
        </intent-filter>
    </activity>
    <activity
        android:name=".SplashActivity"
        android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>



from Toolbar won't disappear in action mode

No comments:

Post a Comment