1

I am attempting to display a Snackbar but is not visible within the fragment, it can find the coordinator layout as evidenced by the FAB moving up (see screenshots below), but it is seemingly displaying behind the view.

Basically what happens is when a task is added, a message is sent back in the bundle to the dashboard fragment at which point it would be displayed.

Keep in mind that snackbar messages are displayed in this same manner throughout the application without issue.

Empty Dashboard No Tasks Found

After task is added, snackbar needs to be displayed

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools">
<data>
    <variable name="taskViewModel" type="com.pomodorocentral.task.dashboard.TaskViewModel"/>
</data>
<androidx.coordinatorlayout.widget.CoordinatorLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/task_coordinator_layout"
        tools:context="com.pomodorocentral.task.dashboard.TaskFragment"
        android:theme="@style/MyTheme.DayNight.NoActionBar"
        android:background="?android:windowBackground"
        android:layout_marginTop="50dp"
        android:layout_marginBottom="50dp">
    <androidx.appcompat.widget.Toolbar
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:theme="@style/ToolBarStyle"
            app:popupTheme="@style/ToolBarStyle.Popup"
            android:id="@+id/task_toolbar"
            android:elevation="4dp"
            app:subtitle="Show Tasks"
            android:layout_marginTop="50dp">
        <com.google.android.material.chip.ChipGroup
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/task_status_filter_group"
                android:visibility="visible"
                app:chipSpacing="4dp"
                android:layout_margin="16dp">
            <com.google.android.material.chip.Chip
                    style="@style/CustomChip"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/active_status_label"
                    android:id="@+id/active_status_chip"
                    app:checkedIconEnabled="true"
                    app:checkedIcon="@drawable/ic_check_white_24dp"
                    app:chipEndPadding="10dp"
                    app:chipStartPadding="10dp"
                    android:includeFontPadding="false"
                    android:elegantTextHeight="false"/>
            <com.google.android.material.chip.Chip
                    style="@style/CustomChip"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/completed_status_label"
                    android:id="@+id/completed_status_chip"
                    app:checkedIconEnabled="true"
                    app:checkedIcon="@drawable/ic_check_white_24dp"
            />
        </com.google.android.material.chip.ChipGroup>
    </androidx.appcompat.widget.Toolbar>
    <LinearLayout
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/task_empty_view"
            android:paddingTop="70dp"
            android:paddingBottom="15dp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintBottom_toTopOf="@+id/addTaskButton"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            android:paddingLeft="8dp"
            android:paddingRight="8dp"
            app:layout_constraintHorizontal_bias="0.0"
            android:theme="@style/MyTheme.DayNight"
            android:visibility="invisible"
            android:layout_marginTop="50dp">
        <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                tools:layout_editor_absoluteY="30dp"
                tools:layout_editor_absoluteX="121dp"
                android:id="@+id/task_empty_heading"
                tools:text="@string/task_empty_heading" android:textAppearance="@style/TextAppearance.MaterialComponents.Headline5"
                android:gravity="center"
                android:visibility="visible"
                android:text="@string/task_empty_heading"
        />
        <TextView
                android:layout_width="match_parent"
                android:layout_height="40dp"
                android:id="@+id/task_empty_text"
                android:textAppearance="@style/TextAppearance.MaterialComponents.Caption"
                tools:text="@string/task_empty_text"
                android:visibility="visible"
                android:text="@string/task_empty_text"
                android:paddingLeft="5dp"
                android:paddingRight="5dp"
                android:gravity="center_vertical|center"/>
        <ImageView
                app:srcCompat="@drawable/ic_pomodoro_shrug"
                android:layout_width="match_parent"
                android:layout_height="340dp"
                android:id="@+id/task_empty_image"
                android:scaleType="fitCenter"
                android:adjustViewBounds="true"
                android:cropToPadding="true"/>
    </LinearLayout>
    <LinearLayout
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginTop="100dp">
        <androidx.recyclerview.widget.RecyclerView
                android:id="@+id/task_recycler_view"
                android:scrollbars="vertical"
                android:layout_width="match_parent"
                android:layout_height="match_parent">
        </androidx.recyclerview.widget.RecyclerView>
    </LinearLayout>
    <com.google.android.material.floatingactionbutton.FloatingActionButton
            app:srcCompat="@drawable/ic_add_black_24dp"
            android:layout_width="wrap_content"
            android:layout_height="78dp"
            android:clickable="true"
            android:id="@+id/addTaskButton"
            android:layout_gravity="end|bottom"
            android:focusable="true"
            android:layout_marginBottom="70dp"
            android:layout_marginRight="16dp"/>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
</layout>

Code for sending snackbar message to dashboard

binding.taskViewModel?.task?.value?.let {
    val bundle = Bundle()
    bundle.putParcelable("message", SnackbarMessage(R.string.new_task_save, Snackbar.LENGTH_SHORT))

    if (binding.taskViewModel?.task?.value?.id == null || binding.taskViewModel?.task?.value?.id == 0) {
        binding.taskViewModel?.create(it)
        navigation.navigate(R.id.action_addTaskFragment_to_action_tasks, bundle)
    } else {
        binding.taskViewModel?.update(it)
        navigation.navigate(R.id.action_editTaskFragment_to_action_tasks, bundle)
    }
}

Code for displaying snackbar (onResume)

arguments?.getParcelable<SnackbarMessage>("message")?.let {
    showSnackbar(task_coordinator_layout, it.resourceId, it.duration)
    arguments?.remove("message")
}

Extension Function which is called to display the snackbar

fun Fragment.showSnackbar(view: CoordinatorLayout?, @StringRes resId: Int, 
@Snackbar.Duration duration: Int) {
    view?.let {
        Snackbar.make(it, it.resources.getString(resId), duration).show()
    }
}

Thoughts?

broot02
  • 103
  • 1
  • 2
  • 11
  • Can you try this? `showSnackbar(activity!!.window.decorView.rootView , it.resourceId, it.duration)` – Naitik Soni Jul 02 '19 at 05:46
  • Yes, I can try this. Wouldn't this cause issues with the coordinator layout, meaning it would fix the visibility, but would no longer move the FAB up when the snackbar is displayed? – broot02 Jul 02 '19 at 12:47
  • Not sure right now...but tell me the result after trying. we can work on it. – Naitik Soni Jul 02 '19 at 13:09
  • @NaitikSoni It worked to show the snackbar, but as I suspected it no longer shows above the bottom navigation bar, and below the FAB. This is not ideal. – broot02 Jul 03 '19 at 10:22
  • See this will help you. https://stackoverflow.com/questions/36332487/move-snackbar-above-the-bottom-bar and https://android.jlelse.eu/scroll-your-bottom-navigation-view-away-with-10-lines-of-code-346f1ed40e9e – Naitik Soni Jul 03 '19 at 10:47
  • Hey....add bottom margin to your recyclerview...it should work. add `android:layout_marginBottom="?attr/actionBarSize"` in xml. Snackbar is hiding behind recyclerview. – Naitik Soni Jul 03 '19 at 13:03

1 Answers1

1

I incrementally made changes, adding margins to the recycler view (they should probably be present regardless), it had no effect. The only change that had any effect was ensuring that the root view was the Coordinator Layout.

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout
    xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/task_coordinator_layout"
    tools:context="com.pomodorocentral.task.dashboard.TaskFragment"
    android:theme="@style/MyTheme.DayNight.NoActionBar"
    android:background="?android:windowBackground"
    android:layout_marginTop="50dp"
    android:layout_marginBottom="50dp">
<androidx.appcompat.widget.Toolbar
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:theme="@style/ToolBarStyle"
        app:popupTheme="@style/ToolBarStyle.Popup"
        android:id="@+id/task_toolbar"
        android:elevation="4dp"
        app:subtitle="Show Tasks">
    <com.google.android.material.chip.ChipGroup
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/task_status_filter_group"
            android:visibility="visible"
            app:chipSpacing="4dp"
            android:layout_margin="16dp">
        <com.google.android.material.chip.Chip
                style="@style/CustomChip"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/active_status_label"
                android:id="@+id/active_status_chip"
                app:checkedIconEnabled="true"
                app:checkedIcon="@drawable/ic_check_white_24dp"
                app:chipEndPadding="10dp"
                app:chipStartPadding="10dp"
                android:includeFontPadding="false"
                android:elegantTextHeight="false"/>
        <com.google.android.material.chip.Chip
                style="@style/CustomChip"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/completed_status_label"
                android:id="@+id/completed_status_chip"
                app:checkedIconEnabled="true"
                app:checkedIcon="@drawable/ic_check_white_24dp"
        />
    </com.google.android.material.chip.ChipGroup>
</androidx.appcompat.widget.Toolbar>
<LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/task_empty_view"
        android:paddingTop="70dp"
        android:paddingBottom="15dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintBottom_toTopOf="@+id/addTaskButton"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        android:paddingLeft="8dp"
        android:paddingRight="8dp"
        app:layout_constraintHorizontal_bias="0.0"
        android:theme="@style/MyTheme.DayNight"
        android:visibility="invisible"
        android:layout_marginTop="50dp">
    <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            tools:layout_editor_absoluteY="30dp"
            tools:layout_editor_absoluteX="121dp"
            android:id="@+id/task_empty_heading"
            tools:text="@string/task_empty_heading"
            android:textAppearance="@style/TextAppearance.MaterialComponents.Headline5"
            android:gravity="center"
            android:visibility="visible"
            android:text="@string/task_empty_heading"
    />
    <TextView
            android:layout_width="match_parent"
            android:layout_height="40dp"
            android:id="@+id/task_empty_text"
            android:textAppearance="@style/TextAppearance.MaterialComponents.Caption"
            tools:text="@string/task_empty_text"
            android:visibility="visible"
            android:text="@string/task_empty_text"
            android:paddingLeft="5dp"
            android:paddingRight="5dp"
            android:gravity="center_vertical|center"/>
    <ImageView
            app:srcCompat="@drawable/ic_pomodoro_shrug"
            android:layout_width="match_parent"
            android:layout_height="340dp"
            android:id="@+id/task_empty_image"
            android:scaleType="fitCenter"
            android:adjustViewBounds="true"
            android:cropToPadding="true"/>
</LinearLayout>
<androidx.recyclerview.widget.RecyclerView
        android:layout_marginTop="?attr/actionBarSize"
        android:layout_marginBottom="?attr/actionBarSize"
        android:id="@+id/task_recycler_view"
        android:scrollbars="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
<com.google.android.material.floatingactionbutton.FloatingActionButton
        app:srcCompat="@drawable/ic_add_black_24dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:clickable="true"
        android:id="@+id/addTaskButton"
        android:layout_gravity="end|bottom"
        android:focusable="true"
        android:layout_margin="16dp"/>
</androidx.coordinatorlayout.widget.CoordinatorLayout>

After doing this everything worked as expected. I wish I knew the true reason why, but quite frankly I've burnt far too much time trying to get this fixed. In a nutshell the fix was removing the layout(databinding) from this view, which is fine for this view, as the databinding had no real use here.

broot02
  • 103
  • 1
  • 2
  • 11