Search code examples
javaandroidandroid-collapsingtoolbarlayoutandroid-appbarlayout

How to get maximum vertical offset of collapsing toolbar layout


I currently have an AppBarLayout with a CollapsingToolbarLayout inside and I have added an OffSetChangeListener so I can rotate the dropdown arrow in my appbar when the actionbar is expanded, now everything works well except that I need the maximum vertical offset to be able to calculate the degrees to rotate and it is not provided in the listener as argument. This is my code :

appBarLayout.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() {
    @Override
    public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
        int slideOffset = verticalOffset*(-1);
        int slideOffsetMax = 750;

        int angle = slideOffset * 180 / slideOffsetMax;
        dropdownArrow.setRotation(angle);

    }

});

The problem is the Maximum Vertical offset varies depending on the device but if anyone has another way of achieving this feel free to show me. ps : I set my maximum offset to 750 because I logged the offset to find the maximum for my device but on my tablet its different.


Solution

  • Use :

    int totalScrollRange = appBarLayout.getTotalScrollRange();
    

    to get the scrollrange of your AppBarLayout and do your calculations.