Search code examples
androidandroid-layoutandroid-dialogfragmentdialogfragment

Android: Apply ActionMode to a DialogFragment


I'm having some trouble using ActionMode with a DialogFragment and hope someone can point me to a solution.

When I start actionmode in my DialogFragment the actionmode is applied to the parent fragment/activity and not the toolbar in the DialogFragment (screenshot below):

Screenshot of the problem

How is it possible to have actionmode applied to the toolbar in the DialogFragment layout.

Here's the code for my DialogFragment:

class WorklistDialog : NetworkDialog(), WorklistDialogContract.View {

    private var actionModeCallback: WorklistDialogActionModeCallback? = null
    override var isInActionMode = actionModeCallback == null
    @Inject lateinit var presenter: WorklistDialogContract.Presenter

...

    override fun startActionMode() {
        actionModeCallback = WorklistDialogActionModeCallback()
        // dialog.window.decorView.startActionMode(actionModeCallback)
        mView?.multiselectview_recycler_view?.startActionMode(actionModeCallback)
    }

...

}

Here's the xml for my main layout:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/dialog_worklist_layout"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <android.support.v4.widget.SwipeRefreshLayout
        android:id="@+id/dialog_worklist_swiperefreshlayout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        app:layout_constraintTop_toBottomOf="@id/dialog_worklist_toolbar">

        <com.conhea.smartgfr.ui.examination.layouts.MultiSelectView
            android:id="@+id/dialog_worklist_multiselectview"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">
        </com.conhea.smartgfr.ui.examination.layouts.MultiSelectView>
    </android.support.v4.widget.SwipeRefreshLayout>

    <android.support.v7.widget.Toolbar
        android:id="@+id/dialog_worklist_toolbar"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:background="@color/colorPrimary"
        android:elevation="4dp"
        android:minHeight="?android:attr/actionBarSize"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>

Here's the xml for the custom view MultiSelectLayout:

<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:tools="http://schemas.android.com/tools">

    <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <CheckBox
            android:id="@+id/multiselectview_select_all_checkbox"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:layout_marginEnd="24dp"
            android:layout_marginStart="24dp"
            android:layout_marginTop="8dp"
            android:layout_weight="1"
            android:text="@string/multi_select_view_select_all"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <android.support.v7.widget.RecyclerView
            android:id="@+id/multiselectview_recycler_view"
            android:layout_width="0dp"
            android:layout_height="0dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/multiselectview_select_all_checkbox"
            tools:listitem="@layout/item_dialog_worklist" />

    </android.support.constraint.ConstraintLayout>
</merge>

Solution

  • The answer was quite simple. Just apply a different look to the toolbar that resembles ActionMode.

    WorklistDialog.kt:

    class WorklistDialog : NetworkDialog(), WorklistDialogContract.View {
    
        ...
    
        override fun startSelectionMode() {
            mView?.dialog_worklist_toolbar?.apply {
                setBackgroundColor(ResourcesCompat.getColor(resources, R.color.white, null))
                setNavigationIcon(ic_arrow_back_black_24dp)
                setNavigationOnClickListener { stopSelectionMode() }
                setTitleTextColor(ResourcesCompat.getColor(resources, R.color.black, null))
            }
        }
    
        override fun updateSelectedCount() {
            mView?.dialog_worklist_toolbar?.apply {
                title = adapter.getSelectedItems().size.toString()
            }
        }
    
        override fun stopSelectionMode() {
            setupToolbar()
        }
    
    ...
    
    }
    

    Screenshot of result:

    enter image description here