4

I was using Collapsible Toolbar in my app. On activity launch Collapsible Toolbar is expanded state with scrolling enabled and its working well normally. But now I have a requirement to show a full screen error layout in case my API fails. In that case I have to collapsed toolbar with scrolling effect blocked.

Error Layout shows a Retry Button. On Retry I make API call again and if API gives success I have to again expand Toolbar and enable scrolling effect.

I was able to collapse toolbar with setExpanded(flag, animate) but in that case I am not able to block scrolling effect of Collapsible Toolbar while error layout is shown.

I need to provide a way to block as well as unblock scroll effect + Expand/Collapse Toolbar. Any help would be really appreciated.. !!!

Pravin Divraniya
  • 4,223
  • 2
  • 32
  • 49
ajay
  • 886
  • 2
  • 12
  • 27

5 Answers5

2

Make your error layout such that it will overlap Collapsible Toolbar. Also set android:clickable="true" to your error layout.

When you set visibility to your error layout, set Toolbar scrolling accordingly.

  <RelativeLayout
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:background="#f3f3f3"
      android:orientation="vertical"
      >
<!-- Add your other layout including Collapsible Toolbar here.-->

<RelativeLayout
      android:id="@+id/errorLayout"
      android:clickable="true"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      />

</RelativeLayout>
Kishan Vaghela
  • 7,678
  • 5
  • 42
  • 67
1

I created a library AppBarrr to lock the screen in expanded mode, based on my previous answer.

As I said, the height of the Toolbar is the key: the CollapsingToolbarLayout will collapse until the Toolbar's height and will expand until the AppBarLayout's height.

With this library, you must set two layouts as the Toolbar and your Expanded Layout (used to lock the screen and the scroll), it will create a CollapsingToolbarLayout and inflate these layouts inside.

You can declare the animations duration, the color of the inner CollapsingToolbarLayout, the collapsed/expanded title's style, even the height of the locked layout... You could also hide the Expanded Layout if you click outside it. It can support NestedScrollView and ScrollView inside the Expanded Layout. The documentation and a sample app are available on Github.


For those who don't want to use the library, my previous answer shows the way to do it. Here's the output of the previous answer:

Prevent toolbar to expand with custom error layout

Basically, this is the same concept, but no need to write a full class, with the lib you just need to have a simple widget in xml and that's it!


Feel free to use, fork or test. Hope it will be useful ;)

Community
  • 1
  • 1
Blo
  • 11,903
  • 5
  • 45
  • 99
  • Hi Fllo, I appreciate your help. Can you please provide full code. Please upload code to git. – ajay Feb 05 '17 at 18:44
  • Your code is not showing how to enable back scrolling after we expand toolbar again. Can you please share code snippet for that too. – ajay Feb 10 '17 at 12:38
0

If you use AlertDialog to communicate the error and a ProgressDialog (spinner) to show you are doing stuff, you can block user input while your app is doing it's thing.

Community
  • 1
  • 1
  • My requirement states to use a error layout instead. Thats why I cannot use alert dialog here. – ajay Dec 19 '16 at 15:08
  • Can you talk this over with the client maybe? AlertDialog is made for stuff like this, and creating something else goes against the Android experience. (it never stopped one from trying something else tho) – Flummox - don't be evil SE Dec 19 '16 at 15:17
  • Alert Dialog is not required as it would dismiss and screen will be shown blank. We need a error layout instead with a retry button to hit API again. – ajay Dec 19 '16 at 15:20
0

A simple solution that you can apply is just use the property

android:visibility="gone" for the content that you don't want to show and just make your error layout visible by using property android:visibility="visible"

place the error layout at the bottom of your parent layout

once the contents are not visible on screen and error layout is just visible you will achieve the desired result that you want. Hope this helps you.

  • but scrolling is not getting disabled in this case also. I am able to Expand/Collapse Collapsible toolbar in this case. – ajay Dec 23 '16 at 04:47
0

You can implement the interface and call its methods when to enable or disable the collapsing effect.

public interface AppbarRequestListener {
    void unlockAppBarOpen();

    void lockAppBarClosed();
}

 @Override
    public void unlockAppBarOpen() {
        appBarLayout.setExpanded(true, false);
        appBarLayout.setActivated(true);
        setAppBarDragging(false);
    }

    @Override
    public void lockAppBarClosed() {
        appBarLayout.setExpanded(false, false);
        appBarLayout.setActivated(false);
        setAppBarDragging(false);

    }

    private void setAppBarDragging(final boolean isEnabled) {
        CoordinatorLayout.LayoutParams params =
                (CoordinatorLayout.LayoutParams) appBarLayout.getLayoutParams();
        AppBarLayout.Behavior behavior = new AppBarLayout.Behavior();
        behavior.setDragCallback(new AppBarLayout.Behavior.DragCallback() {
            @Override
            public boolean canDrag(AppBarLayout appBarLayout) {
                return isEnabled;
            }
        });
        params.setBehavior(behavior);
    } 
Usman Rana
  • 2,067
  • 1
  • 21
  • 32
  • animation stops working when calling `setAppBarDragging` and ` appBarLayout.setExpanded(true, true);` after that – user924 Sep 30 '19 at 21:25