Second toolbar's scrollFlag attribute is applied by the first toolbar
Asked Answered
Z

1

6

Scenario:

When one of the toolbars is gone, the second toolbar will adapt the scrollFlag of the first toolbar, instead of its own scrollFlag!

Note:

The weird behavior happens only when one of the toolbars are gone, if both of them are visible, the scrollFlag attribute is set correctly to each of the toolbars.

Example 1:

    <android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <android.support.v7.widget.Toolbar
        android:id="@+id/appbar_edit_toolbar"
        android:visibility="gone"
        android:layout_width="match_parent"
        android:layout_height="?actionBarSize"
        android:background="@color/accent"
        app:contentInsetStart="@dimen/content_inset"
        app:navigationIcon="@drawable/ic_clear"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
        app:titleTextColor="@android:color/white"
        tools:ignore="UnusedAttribute" />

    <android.support.v7.widget.Toolbar
        android:id="@+id/appbar_normal_toolbar"
        android:layout_width="match_parent"
        android:layout_height="?actionBarSize"
        android:background="?colorPrimary"
        app:layout_scrollFlags="scroll|enterAlways"
        app:contentInsetStart="@dimen/content_inset"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
        app:title="@string/app_name"
        tools:ignore="UnusedAttribute" />

</android.support.design.widget.AppBarLayout>

When running the app in that case, when one of the toolbars is shown, the scrollFlag behavior of it is as appbar_edit_toolbar (i.e: default behaviour which is show always when scrolling), instead of when appbar_normal_toolbar is shown, its scrollFlag behavior should be scroll|enterAlways as in the xml.

Example 2:

    <android.support.v7.widget.Toolbar
        android:id="@+id/appbar_normal_toolbar"
        android:layout_width="match_parent"
        android:layout_height="?actionBarSize"
        android:background="?colorPrimary"
        app:layout_scrollFlags="scroll|enterAlways"
        app:contentInsetStart="@dimen/content_inset"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
        app:title="@string/app_name"
        tools:ignore="UnusedAttribute" />

    <android.support.v7.widget.Toolbar
        android:id="@+id/appbar_edit_toolbar"
        android:visibility="gone"
        android:layout_width="match_parent"
        android:layout_height="?actionBarSize"
        android:background="@color/accent"
        app:contentInsetStart="@dimen/content_inset"
        app:navigationIcon="@drawable/ic_clear"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
        app:titleTextColor="@android:color/white"
        tools:ignore="UnusedAttribute" />

</android.support.design.widget.AppBarLayout>

When running the app in that case, when one of the toolbars is shown, the scrollFlag behavior of it is as in appbar_normal_toolbar (i.e: scroll|enterAlways), instead of when appbar_edit_toolbar is shown, its scrollFlag behavior should be default (i.e show always).

I tried setting the scrollFlags attribute dynamically, invalidating the AppBarLayout, but that wouldn't work.

I'm pretty sure this behavior happens because of a bug in Android libraries, as they don't expect me to disappear one of the toolbars.

What do you guys think? Any other way to have make my activity have 2 different toolbars that only one of them is visible at a time?

Zootechnics answered 17/1, 2018 at 22:22 Comment(7)
why you need two toolbars in the same page. !!Checker
@ragu-swaminatahan my activity handles a "mode" flag which activates the first toolbar or the second one. Each of them has a different look and menu to inflate.Zootechnics
you can use single toolbar and handle them dynamically in code, which is much simplerChecker
@Zootechnics whats problem with your code. For me getting correct behaviour of scrollflagsBerkeley
@RaguSwaminathan Using a single toolbar is not very "by design" solution, as I said, each of the toolbars represent a different state of my activity and they have a different look and menu items.Zootechnics
@MohamedMohaideenAH Do you have a recyclerview or a scrollable view in your activity, so you can see how the scrollFlags are behaving? I will be glad if you could show me a working example codeZootechnics
@Zootechnics Yes am using NestedScrollview below multiple toolbar. Here you can see the results. With Multiple Toolbar & With ScrollToolbar Only & With FixedToolbar Only. Am using same xml code what you are posted for toolbars.Berkeley
B
0

For me working correct. Check this out same code.

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="md.com.androidui.MutipleToolbar">

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/AppTheme.AppBarOverlay">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar1"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:title="Scrolled Toolbar"
        app:layout_scrollFlags="scroll|enterAlways"
        app:popupTheme="@style/AppTheme.PopupOverlay" />

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar2"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorAccent"
        app:title="Fixed Toolbar"
        app:popupTheme="@style/AppTheme.PopupOverlay" />

</android.support.design.widget.AppBarLayout>

<android.support.v4.widget.NestedScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">

    <include layout="@layout/content_mutiple_toolbar" />

</android.support.v4.widget.NestedScrollView>

Output Case 1: Both Visible

enter image description here

Case 2: Scrolled toolbar only visible

enter image description here

Case 3: Fixed toolbar only visible

enter image description here

Berkeley answered 27/1, 2018 at 16:34 Comment(2)
Is there any chance you please upload your entire project anywhere? so I can test it directly? Cheers!Zootechnics
I didn't do more in that project. Thats sample created project. I just make new project then tested with it.Berkeley

© 2022 - 2024 — McMap. All rights reserved.